Visual Studio 2015 - Xamarin - Android - Getting "resource.id does not contain a definition for xxx" when I try to do anything in the .cs file

23,675

Solution 1

(VS 2019) I did the previous solutions and nothing, I had the same problem and the solution for me was to remove '&' from the text of TextView, I was trying to Set the text to "text&text" even &&(I though it was like mnemonics as in WindowsForms) didn't work so I had to remove it and it fixed.

Solution 2

I changed the text field "id" from "@+id/imageView1" to "1", saved, rebuilt and changed it back to "@+id/imageView1", it fixed it. I would like to add, this whole problem came from

  1. Adding a new activity and layout.
  2. Using preexisting code from another app that I had.
  3. Copying and pasting code from the original app to the new Activity and Layout.

All fairly common stuff, the real problem seemed always to be adding any new Activities and Layouts to a main Activity. It can get very convoluted and with no (known to me) logical way to run down a problem with Xamarin. Don't get me wrong compared to 10 years ago (the last mobile app I tried to write) Xamarin is heaven. Good coding folks, now if I can only figure out why Keyword "this" is error-ring on the added Activity.cs

Solution 3

For people who are still facing this issue, the default Build Action of the layout file would be set to TransformFile. Select the layout, go to the Layout Properties, and in the properties pane, Select AndroidResource as your Build Action. Clean build your project and it should work.

Solution 4

Just add namespace like that Android.Resource.Id - it's resolve for me

Solution 5

What did work for me (Visual Studio 2017, opening an old Xamarin project):

  1. Delete obj and bin folders, build.
  2. If errors, restart Visual Studio (I know the pain).
  3. Build again

Now the Resource will be visible (of course, if you defined it correctly).

Share:
23,675
RobertM - Orlando
Author by

RobertM - Orlando

Updated on July 09, 2022

Comments

  • RobertM - Orlando
    RobertM - Orlando almost 2 years

    Adding Additional Activity .cs and Layout axml Using Visual Studio 2015.

    I'm very new to Xamarin and Android development, but have been a developer for a few years using VB and now C#. I have a simple app on Android 4.2 that is getting more complicated as I go along. The simple matter us that I want to add an additional GpsAction.cs and corresponding Gps.axml layout to the project. It seems impossible to find the right combination syntax to achive this. I have a mainActivity with main.axml. In VS 2015 it's very simple to add new but I keep getting "resource.id does not contain a definition for" I would really appreciate your help with this

    namespace AddCam
    {
    [Activity(Label = "GpsActivity")]
    public class GpsActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.GpsLayout);
    
            string c = FindViewById<TextView>(**Resource.Id.textView1**).Text;
            // Create your application here
        }
    }
    
  • Pavel
    Pavel about 7 years
    Thank you a lot! I've just added public partial class Id { } inside class Resource and rebuilt the project. Now all Resource.Id.____ works well
  • Zun
    Zun almost 6 years
    really bad advise. The Designer class was designed to prevent manually entering IDs
  • Assasin Dentes
    Assasin Dentes over 5 years
    Well, then one should remember SAVING the main.axml file before moving to the .cs file. That's what I found though :p
  • Pac0
    Pac0 almost 5 years
    This one, done along with clean rebuild and RoberM's answer did the work. VS 2017 + Xamarin Forms 3.6.0