第二个按钮将无法与Xamarin c#android应用程序中的事件处理程序一起工作



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 (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);     
        };

    }
}

}

资源设计者内部:

public partial class Id
    {
        // aapt resource value: 0x7f050001
        public const int button1 = 2131034113;
        // aapt resource value: 0x7f050000
        public const int myButton = 2131034112;

第二个按钮的设置与第一个按钮完全相同,但单击时它什么也不做,而第一个按钮会打开网页。第二个按钮的ID为@+id/button1

感谢

将每个按钮写得更靠近其事件处理程序,如下所示:

Button btn1 = FindViewById<Button>(Resource.Id.button1);
btn.Click += (object se,EventArgs e)
{
}

相关内容

  • 没有找到相关文章

最新更新