第二个按钮不能与Xamarin c#android应用程序中的事件处理程序一起使用
MainActivity.cs第二个按钮不能与Xamarin c#android应用程序中的事件处理程序一起使用,c#,android,button,event-handling,C#,Android,Button,Event Handling,MainActivity.cs namespace testApp { [Activity (Label = "DHS HotKeys", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { protected override void OnCreate (Bundle bundle) { base.OnCreate
namespace testApp
{
[Activity (Label = "DHS HotKeys", MainLauncher = true, Icon = "@drawable/icon")]
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);
// Get our button from the layout resource,
// and attach an event to it
Button button1 = FindViewById<Button> (Resource.Id.button1);
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
var uri = Android.Net.Uri.Parse ("https://www.fridayparentportal.com/delran/");
var intent = new Intent (Intent.ActionView, uri);
StartActivity (intent);
};
button1.Click += (object sender, EventArgs e) => {
var uri2 = Android.Net.Uri.Parse ("http://dhs.delranschools.org/students/lunch_menu/");
var intent2 = new Intent (Intent.ActionView, uri2);
StartActivity (intent2);
};
}
}
第二个按钮的设置与第一个按钮完全相同,但单击时它什么也不做,而第一个按钮打开网页。第二个按钮的ID为@+ID/button1
谢谢将每个按钮写得离其eventhandler更近,如下所示:
Button btn1 = FindViewById<Button>(Resource.Id.button1);
btn.Click += (object se,EventArgs e)
{
}
Button btn1=findviewbyd(Resource.Id.button1);
点击+=(对象se,事件参数e)
{
}
2号按钮的onClick方法正在执行?我在键入该方法后意识到您的意思,并在按钮1上添加了一个断点单击。问题是我每次都在构建一个版本,而不是调试模式。对不起,我是新手。
Button btn1 = FindViewById<Button>(Resource.Id.button1);
btn.Click += (object se,EventArgs e)
{
}