单击处理程序 Xamarin

mac*_*688 5 xamarin.ios xamarin

我正在尝试在我的 Xamarin.iOS 应用程序中注册单击事件。我不想在这样的方法中注册事件。

http://developer.xamarin.com/recipes/ios/standard_controls/buttons/handle_clicks/

我想从情节提要中注册它们,似乎有功能可以做到这一点。

尝试将事件绑定到控制器中的方法

在我的 ViewController 中我有以下代码:

    public void ButtonPressed(object sender, EventArgs ea)
    {
        UIAlertView error = new UIAlertView("Test Title", "This is a test", null, "Ok", null);
        error.Show();
    }
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序并按下按钮时,我在 appDelegate 中收到错误

Foundation.MonoTouchException:抛出 Objective-C 异常。名称:NSInvalidArgumentException 原因:-[HomeViewController ButtonPressed:]:无法识别的选择器发送到实例 0x14691650

如何注册点击事件?

Iai*_*ith 4

检查你的 ViewController 的设计器文件,它会有这样的内容:

using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;

namespace TEST
{
    [Register ("ViewController")]
    partial class ViewController
    {
        [Action ("ButtonPressed:")]
        [GeneratedCode ("iOS Designer", "1.0")]
        partial void ButtonPressed (UIButton sender);

        void ReleaseDesignerOutlets ()
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以只需像这样实现该方法:

using System;
using UIKit;

namespace TEST
{
    public partial class ViewController : UIViewController
    {
        public ViewController (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
        }

        partial void ButtonPressed (UIButton sender)
        {
            Console.Write ("HEY mark this answer as accepted");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)