C# 永远无法访问Xamarin Android Main活动代码
我是Xamarin的新手,我尝试构建以下程序:C# 永远无法访问Xamarin Android Main活动代码,c#,android,xamarin,C#,Android,Xamarin,我是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); Bu
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!!";
}
private TextView txtShow;
int计数器=0;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.MainPage);
按钮btnShowText=findviewbyd(Resource.Id.btnShowText);
txtShow=findviewbyd(Resource.Id.txtShow);
btnShowText.Click+=btnShowText\u Click;
}
私有void BtnShowText_单击(对象发送者,事件参数e)
{
txtShow.Text=“这是您的“+计数器+++”。单击!!”;
}
但问题是,当我调试时,这个代码永远不会到达
点击按钮的结果是,按钮文本变为“2次点击”、“3次点击”
我想这个按钮仍然使用这个代码,它来自空白应用程序项目的例子,虽然我改变了按钮ID。这怎么会发生?
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
Button-Button=findviewbyd(Resource.Id.MyButton);
button.Click+=delegate{button.Text=string.Format(“{0}clicks!”,count++);};
您试图将文本视图
强制转换为编辑文本
:
这一行在这里:
private TextView txtShow;
在这里:
txtShow = FindViewById<EditText>(Resource.Id.txtShow);
txtShow=findviewbyd(Resource.Id.txtShow);
根据需要或xaml中的内容,将其更改为
TextView
或EditView
。在解决方案上执行Clean All
和Build All
,然后再次调试应用程序。如果仍然存在问题,请删除输出bin
和obj
目录,然后再次执行buildall
。调试时执行是否达到OnCreate方法?我认为这一定是一个错误,因为创建新项目后,它现在可以正常工作。非常感谢。