Xamarin Resource.Id doesn’t recognize

Issue

I’ve created new project (blank app) in Xamarin, but in the MainActivity.cs preset code are missing the button and SetContentView is commented. So i’ve added manually the code, but when i create the button using FindViewById he doesn’t recognise Resource.Id and he mark .id as an error

Resource.designer.cs

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")]
public partial class Resource
{

    static Resource()
    {
        global::Android.Runtime.ResourceIdManager.UpdateIdValues();
    }

    public static void UpdateIdValues()
    {
    }

    public partial class Attribute
    {

        static Attribute()
        {
            global::Android.Runtime.ResourceIdManager.UpdateIdValues();
        }

        private Attribute()
        {
        }
    }

    public partial class Drawable
    {

        // aapt resource value: 0x7f020000
        public const int Icon  2130837504;

        static Drawable()
        {
            global::Android.Runtime.ResourceIdManager.UpdateIdValues();
        }

        private Drawable()
        {
        }
    }

    public partial class Layout
    {

        // aapt resource value: 0x7f030000
        public const int Main  2130903040;

        static Layout()
        {
            global::Android.Runtime.ResourceIdManager.UpdateIdValues();
        }

        private Layout()
        {
        }
    }

    public partial class String
    {

        // aapt resource value: 0x7f040001
        public const int ApplicationName  2130968577;

        // aapt resource value: 0x7f040000
        public const int Hello  2130968576;

        static String()
        {
            global::Android.Runtime.ResourceIdManager.UpdateIdValues();
        }

        private String()
        {
        }
    }
}

MainActivty.cs

using System;
using Android.Runtime;
using Android.Views;
using Android.App;
using Android.Widget;
using Android.OS;

namespace App1
{
    [Activity(Label  "App1", MainLauncher  true, Icon  "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        Button button  FindViewById<Button>(Resource.Id.MyButton);
    }
}

Error: “Resource” does not contain a definition for “id”

Screenshot

Solution

Check if the Resource class has been generated by Xamarin. You can find the file (Resource.designer.cs) under your Android project in the Resources folder. If it has not been generated, usually rebuilding the project will generate it again (unless there is a syntax error somewhere).

The Resource.Id class variables are generated for Views that are created in .axml files.

Edit based after the answers in the comments to be complete:

The Resources class should contain an inner Id class, which it apparently doesn’t. This can happen if there is a syntax error in you .axml files, because in that case the Resources.designer.csfile won’t be regenerated. Or you haven’t set an id at all, you would add the id like this: <Button android:id"@+id/myButton" />

Answered By – Tim Klingeleers

Leave a Comment