Visual Studio 2015 - Xamarin - Android - Getting “

2020-02-25 22:29发布

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
    }
}

16条回答
看我几分像从前
2楼-- · 2020-02-25 23:11

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.

查看更多
老娘就宠你
3楼-- · 2020-02-25 23:13

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); .

查看更多
萌系小妹纸
4楼-- · 2020-02-25 23:14

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.

查看更多
Root(大扎)
5楼-- · 2020-02-25 23:18

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

查看更多
We Are One
6楼-- · 2020-02-25 23:18

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

查看更多
在下西门庆
7楼-- · 2020-02-25 23:19

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

查看更多
登录 后发表回答