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:19

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

查看更多
对你真心纯属浪费
3楼-- · 2020-02-25 23:22

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

查看更多
你好瞎i
4楼-- · 2020-02-25 23:22

Like Maniacz said, in VS 2019 I just had to change the XML to axml extensión to the layout in layout folder under resources

查看更多
\"骚年 ilove
5楼-- · 2020-02-25 23:22

For anyone else looking for an answer despite running clean/build/rebuild which didn't work for me:

I had freshly installed a number of tools for xamarin development in VS. Though a build/rebuild may have actually worked, in my case I believe what also fixed it was closing and re-opening VS. I'm pretty sure I had a few issues, primarily stemming from newly installed tools (android SDKs in my case) requiring VS to be restarted. If you're working on a project already having installed the tools you need, try as others have said - build/rebuild.

查看更多
劳资没心,怎么记你
6楼-- · 2020-02-25 23:28

Check if you are missing these namespaces in your 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"
查看更多
▲ chillily
7楼-- · 2020-02-25 23:29

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.

查看更多
登录 后发表回答