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

Adding additional .cs actions and an axml layout using Visual Studio 2015.

I am very new to Xamarin and Android, but have worked as a developer on VB for several years, and now in C #. I have a simple application on Android 4.2, which is becoming more difficult as you move forward. The simple thing is that I want to add an additional GpsAction.cs and the corresponding Gps.axml layout to the project. It seems impossible to find the right combination syntax to achieve this. I have mainActivity with main.axml. In VS 2015 it’s very easy to add new ones, but I keep getting “resource.id does not contain a definition”. 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 } } 
+13
android c # xamarin xamarin.android
source share
15 answers

For people who are still experiencing this problem, the default Build action file layout will be set to TransformFile . Select a layout, go to Layout Properties, and in the properties panel, select AndroidResource as the creation action. Clean your project and it should work.

+4
source share

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

  1. Adding a new action and layout.
  2. Using existing code from another application of mine.
  3. Copy and paste code from the source application into a new action and layout.

All fairly common things, the real problem, it always seemed, was to add any new activities and layouts to the main activity. This can be very confusing and without the (known to me) logical way to solve the problem with Xamarin . Don't get me wrong compared to 10 years ago (the last mobile application I tried to write) Xamarin is a paradise. Good programmers, now if I can only find out why the keyword " this " is a mistake when adding Activity.cs

+7
source share

Just add a namespace like Android.Resource.Id is the solution for me

+4
source share

What worked 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).

+3
source share

You must add the set value for android:id="@+id/button1" in the axml of the application, and then rebuild the project and try again. e.g. Button button = (Button)FindViewById(Resource.Id.button1); .

+1
source share

The Main.axml and Reset page is cleared and it works.

0
source share

The best solution I have found is to build a solution. Choose Build Solution from the Build menu (or Ctrl + Shift + B ). This action will solve the problem.

0
source share

My problem was that I was trying to debug an Android Xamarin project (my own) using Xamarin Live Player, plugging it in with a USB cable and choosing a device to debug, and solved the problem

0
source share

Check if these namespaces are missing in the layout file -

 xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" 
0
source share

Make sure your axml is properly formed and rebuild the solution that should work. If you continue to encounter the same problem, remove axml and add it again, and then create a solution.

0
source share

In Visual Studio 2019, when you add a new Android layout to the project, it is added as an XML file. I already had some previously created layouts in the Resources / layout folder with the extension .axml (not .xml), and for me the extension .xml ->. Axml changed.

0
source share

I understand that this problem is very old, but I ran into it in Visual Studio 2019 and found a solution.

The problem arises, for me, when adding an element to the layout, and then when trying to add code. The problem seems to be related to the way the project is built.

Add an element to the layout, then create your project before adding additional code. Obviously, Resource.Id is not updated with additional members until it is compiled, and tries to access a new element in the code before Resource.Id detects that this prevents the creation of the project.

0
source share

As Maniacs said, in VS 2019, I just needed to change the XML to axml extension to the layout in the layout folder under resources

0
source share

For those looking for an answer, despite running the clean / build / rebuild command, which doesn't work for me:

I recently installed a number of xamarin development tools in VS. Although the build / rebuild may have actually worked, in my case, I believe it also fixed the closing and reopening of VS. I am pretty sure that I had several problems, primarily related to recently installed tools (in my case, Android SDK) requiring a VS reboot. If you are working on a project in which the necessary tools are already installed, try, as others have said, to build / restore.

0
source share

Check out the Resource.Designer class file. There will be a class like public partial class Id . The integer identifiers of the controls are written there. Use them instead of Resource.Id

-one
source share

All Articles