When Resource Ids Are Not Recognised In A CS File

I’m using Xamarin Android for one of my current projects and (especially after my recent short break) sometimes I forget important lessons that could leave me confused and annoyed for ages.

Here’s an example of one of them:

Sometimes you need to create a new component in your axml file. You add an id and the rest of the parameters. Then you want to create the event that this button will call once clicked. So you go to a CS file, create a new variable to hold the resource and proceed to create a new event. Then the compiler catches up and you see a little red squiggly line under your resource id name 😩

Why Does This Happen?

The Resource Library is searched for the id of the new component when the code is pre-complied. The Resource Library is a file in the Android project listing all id’s of your components declared within your axml files. If the id of the component mentioned in the CS file doesn’t exist in the Resource Library, then this is highlighted as an error.

Resource Id Resolution

Comment out the use of the resource in the CS file, then do a rebuild of the project. After that, uncomment the reference of the resource in the CS file. Voila!

Reason

The new resource needs to be added into the Resource Dictionary before it’s used in other files. By commenting out it’s usage in the CS file, when the compiler checks for the resources, it won’t find it anywhere and will continue to add it the Resource Dictionary correctly without freaking out.

Coding is part banging your head against your desk, part shouting at your computer, but mostly (hopefully) part fun. It’s the fun that expires me and keeps me going when I hit those coding walls of despair. But I keep hope alive because I know I can get over it…eventually! If you code something everyday, you’ll start to hit these walls less often.