简体   繁体   中英

'Resource.Layout' does not contain a definition for 'activity_main' in xamarin

I am getting an error of 'Resource.Layout' does not contain a definition for 'activity_main' . But I am confused, why it is showing this type of error. Because activity_main is available in Resource.Layout , but still showing the same error.

Here is the code,

Resources => Layout => activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:minWidth="25px"
    android:minHeight="25px">
    <android.webkit.WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView" />
</LinearLayout>

Resources => MainActivity.cs

[Activity(Label = "BSSTMatrimonial", MainLauncher = true)]
    public class MainActivity : Activity
    {
        WebView webView = null;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
          SetContentView(Resource.Layout.activity_main);

           // webView = FindViewById<WebView>(Resource.Id.webView);
           //webView.SetWebViewClient(new ExtendWebViewClient());

            webView.LoadUrl("www.bsstmatrimony.com");
            WebSettings webSettings = webView.Settings;
            webSettings.JavaScriptEnabled = true;
        }
    }

Try to Clean & Rebuild your solution. Even you can delete & re-add .axml file. If still won't work write click on .axml file

  • Go to properties -->Build action Make sure Build action set to AndroidResource .

It is a harmless error that can be ignored but you can search your files for Resource.Designer.cs. It is likely that you have two, one in your project's obj/debug folder and one in your project's Resources folder. Check for activity_main in both, it is possible that the file in your Resources folder doesn't have it.

I've been encountering this problem a lot. I think the problem lies in the fact that as xamarin is updated, it changes the variable names and file names in the templates. So if you are using code from a tutorial, if you are seeing this problem, its likely outdated. This means that you have to rename something to fit the current template.

In this case I had to change

SetContentView(Resource.Layout.Main);

to

SetContentView(Resource.Layout.activity_main);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM