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
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
- Adding a new activity and layout.
- Using preexisting code from another app that I had.
- 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):
- Delete obj and bin folders, build.
- If errors, restart Visual Studio (I know the pain).
- Build again
Now the Resource will be visible (of course, if you defined it correctly).
RobertM - Orlando
Updated on July 09, 2022Comments
-
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 about 7 yearsThank you a lot! I've just added
public partial class Id { }
inside class Resource and rebuilt the project. Now allResource.Id.____
works well -
Zun almost 6 yearsreally bad advise. The Designer class was designed to prevent manually entering IDs
-
Assasin Dentes over 5 yearsWell, then one should remember SAVING the main.axml file before moving to the .cs file. That's what I found though :p
-
Pac0 almost 5 yearsThis one, done along with clean rebuild and RoberM's answer did the work. VS 2017 + Xamarin Forms 3.6.0