我有一个非常简单的XamarinAndroid应用程序。它有一个文本框和一个按钮。以下是参考资料中的activity\u 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:orientation="vertical">
<TextView
android:text="0"
android:textSize="50sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtNumber"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="Get Number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnAutoGenerate" />
在解决方案资源管理器中,我拥有资产和资源。
在“资源”下是“布局”,在“布局”下是“活动\ u main.axml”
一点也不复杂。
在MainActivity.cs中,OnCreate过程如下:
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TextView txtNumber;
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
txtkNumber = FindViewById<TextView>(Resource.Id.txtNumber);
FindViewById<Button>(Resource.Id.btnAutoGenerate).Click += (o, e) =>
txtNumber.Text = btnAutoGenerate_Click(o, e);
}
我不确定资源是在哪里定义的(在xml中是Resources),但尽管一开始编译时没有问题,但现在我得到了一个错误:“Resource”这个名称在当前上下文中不存在。
因为在第一次生成默认文件时
SetContentView(Resource.Layout.activity_main);