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);
上一篇:UIButton设置图片和文字(一)


下一篇:xamarin ios c#强调UIButton文本