当前位置:首页 > 操作系统 > Ios

c# – Xamarin iOS UIButton如何以编程方式单击按钮?

我试图通过在我的ViewWillAppear()函数中以编程方式调用它来伪造点击按钮的行为.

onclick函数在我的ViewDidLoad()中定义,您可以看到我正在尝试使用Perform Selector手动调用该按钮.

该按钮似乎没有运行.有任何想法吗?

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

    idButtonScanLoad.TouchUpInside += async (sender, ea) =>
    {
        System.Console.WriteLine("Scan button pressed");
    };

}

[Export("TouchUpInsideEvent:")]
private void TouchUpInsideEvent(NSObject sender)
{
    Console.WriteLine("yay!");
}

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    this.PerformSelector(new ObjCRuntime.Selector("TouchUpInsideEvent:"), this as NSObject, 10f);
}

解决方法:

这是答案,您应该从ViewDidLoad方法中调用它.

myButton.SendActionForControlEvents (UIControlEvent.TouchUpInside);

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!