编程语言
首页 > 编程语言> > c# – Xamarin iOS UIButton如何以编程方式单击按钮?

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

标签:c,ios,uibutton,xamarin,performselector
来源: https://codeday.me/bug/20190523/1156679.html