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

前端 未结 17 1476
后悔当初
后悔当初 2020-12-30 07:21

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

I\'m very new to Xamarin and Android

相关标签:
17条回答
  • 2020-12-30 07:32

    I had to modify Build Action for my layout file and re set the original Build Action as it was previously set. This made my visual studio to regenerate the resource ids in Resource.designer.cs file.

    Step #1:
    Go to properties of the layout file that is missing its ids and click the Build Action DropDown.

    Step #2:
    Select something from the dropdown other than AndroidResource.

    Step #3:
    Reselect AndroidResource from that Build Action.

    Now, you will have your Resource.designer.cs file regenerated and it will have the reference ids to your controls in the layout file.

    0 讨论(0)
  • 2020-12-30 07:34

    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

    0 讨论(0)
  • 2020-12-30 07:37

    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.

    0 讨论(0)
  • 2020-12-30 07:38

    In Visual Studio 2019 when you add a new Android Layout to the Project it is added as .xml file. I already had some created earlier layouts in Resources/layout folder with extension .axml (not .xml) and for me changing the extension .xml -> .axml worked.

    0 讨论(0)
  • 2020-12-30 07:39

    I understand that this issue is very old, but I've run into it as well in Visual Studio 2019, and have found a solution.

    The issue occurs, for me, when adding an element to the layout and then attempting to add code. The issue appears to be related to the way the project is built.

    Add the element to your layout, then build your project before adding any additional code. Apparently Resource.Id does not update with additional members until it's built, and attempting to refer to the new member in the code before Resource.Id recognizes it prevents the project from being built.

    0 讨论(0)
  • 2020-12-30 07:40

    You Should add set value forandroid:id="@+id/button1" in axml of app, then rebuild the project and try again. like thisButton button = (Button)FindViewById(Resource.Id.button1); .

    0 讨论(0)
提交回复
热议问题