在创建时设置按钮文本未将文本设置为定义

Set Button Text On Create not set text as define

我有一个代码可以在创建时更改文本

  public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

            Button btnbut = FindViewById<Button>(Resource.Id.btnbut);
            TextView txtvw = FindViewById<TextView>(Resource.Id.txtView);
            txtvw.Text = "Hello World";
            btnbut.Text = "Hello WOrld";
        }
    }

我的main.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout1">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="47.5dp"
        android:id="@+id/txtView"
        android:layout_marginLeft="1.0dp"
        android:layout_marginRight="0.0dp" />
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnbut" />
</LinearLayout>

当我 运行 并使用模拟器打开应用程序时,文本与 main.axml 上的定义保持一致,而不是文本定义 OnCreate.

您的代码是正确的,结果应该是:

尝试对您的解决方案执行 clean all/build all,然后再次在模拟器上重试您的应用程序。

注意:如果仍未正确更新模拟器上的应用程序:

在模拟器中:

Setting / Apps / Select your app / *Uninstall*

我想我得到了你的我不使用 xamarin 但我认为这是错误尝试更改

SetContentView(Resource.Layout.Main);

SetContentView(Resource.Layout.My main);

因为您的布局名称是 "My main" 而不是 "main"