我是 Xamarin 的新手,我尝试构建以下程序:
private TextView txtShow;
int counter = 0;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.MainPage);
Button btnShowText = FindViewById<Button>(Resource.Id.btnShowText);
txtShow = FindViewById<EditText>(Resource.Id.txtShow);
btnShowText.Click += BtnShowText_Click;
}
private void BtnShowText_Click(object sender, EventArgs e)
{
txtShow.Text = "This is your " + counter++ + ". Click!!";
}
但问题是,当我调试时,永远不会到达这段代码。
点击按钮的结果是,按钮文本变为“2 次点击”、“3 次点击”......
我认为该按钮仍然使用此代码,该代码来自空白应用程序示例,尽管我更改了按钮 Id。怎么会这样?
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
最佳答案
您正在尝试将 TextView
转换为 EditText
:
这里是这一行:
private TextView txtShow;
在这里:
txtShow = FindViewById<EditText>(Resource.Id.txtShow);
根据您的需要或 xaml 中的内容,将其更改为 TextView
或 EditView
。
关于c# - 永远不会到达 Xamarin Android Mainactivity 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37040005/