【问题标题】:2nd button will not work with event handler in Xamarin c# android app第二个按钮不适用于 Xamarin c# android 应用程序中的事件处理程序
【发布时间】:2015-02-27 05:36:22
【问题描述】:

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

谢谢

【问题讨论】:

  • 第二个按钮的onClick方法正在执行?
  • 在我输入之后我意识到你的意思,我在 button1 点击上添加了一个断点。问题最终是我每次都在构建一个版本,而不是调试模式。抱歉,我是新手。

标签: c# android button event-handling


【解决方案1】:

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    相关资源
    最近更新 更多