简体   繁体   English

如何在xamarin ios C#中以编程方式触发UIButton的TouchUpInside

[英]How to fire TouchUpInside of UIButton Programmatically in xamarin ios C#

I am trying to fire an touchupinside event of Uibutton programmatically. 我试图以编程方式触发Uibutton的touchupinside事件。 How can i do that ? 我怎样才能做到这一点 ? The code I tried uses PerformSelector but i get this error 我尝试的代码使用PerformSelector,但我收到此错误

"Error MT4117: The registrar found a signature mismatch in the method
 'VCPAckage2.ActivityViewController.TouchUpInsideEvent' - the
 selector 'TouchUpInsideEvent:' indicates the method takes 1
 parameters, while the managed method has 2 parameters. "

I want to achieve something like 我希望实现类似的目标

UIButton.FireEvent("TouchUpInsideEvent") - this will fire the TouchUpInsideEvent

UIButton.PerformSelector(new MonoTouch.ObjCRuntime.Selector ("TouchUpInsideEvent:"), null, 2000f);

Here's the code 这是代码

 private void LoadFn()
 {
    UIButton btnSubmit = new UIButton(new RectangleF(0,0,View.Frame.Width,40));
    btnSubmit.TouchUpInside+=TouchUpInsideEvent;
 }   

 [Export("TouchUpInsideEvent:")]
 private void TouchUpInsideEvent(object sender,EventArgs e){
 float yy = AppConstants.ZeroVal;
 if (FeedbackSubmittedReturnFlag == true) {
        yy =  ChildScrollView2.Subviews[1].Frame.Height+ChildScrollView2.Subviews[1].Frame.Y;
 }
     this.ParentScrollView.SetContentOffset (new PointF (View.Frame.Width, yy), false);
 }

The following code snippet would be suffice 下面的代码片段就足够了

btnObj.SendActionForControlEvents(UIControlEvent.TouchUpInside);

It has to be called from Main thread 它必须从主线程调用

There's a few different things above. 上面有一些不同的事情。

First, the MT4117 is correct. 首先, MT4117是正确的。 It happens because your [Export] attribute specify a selector to a method that has only one parameter (ie it has only one : ) while the managed method has two parameters (which is the default for .NET events). 这是因为你的[Export]属性指定一个选择到只有一个参数的方法(即它只有一个: ),而管理的方法有两个参数(这是.NET事件默认值)。 The registrar will spot such conditions and report errors. 注册服务商将发现此类情况并报告错误。

Second, the PerformSelector methods are bindings over performSelector:... selectors (most are defined on the NSObject protocol , not class). 其次, PerformSelector方法是对performSelector:...选择器的绑定(大多数是在NSObject 协议上定义的,而不是类)。 As such they have the same limitations (eg the number of arguments they can handle). 因此,它们具有相同的限制(例如,它们可以处理的参数数量)。

Third, there are several ways you could call your own code. 第三,有几种方法可以调用您自己的代码。 An easy one would be, like @jonathanpeppers suggested, to directly call your managed method when needed. 就像@jonathanpeppers建议的那样,一个简单的方法就是在需要时直接调用托管方法。

Another one would be to adjust your code to match both 1 and 2 requirements, eg 另一个是调整代码以匹配12要求,例如

// assign one (two parameters) method as a .NET event
btnSubmit.TouchUpInside += TouchUpInsideEvent;
...
// call another (one parameter) method like a selector
any_nsobject.PerformSelector (new Selector ("TouchUpInsideEvent:"), sender as NSObject, 0f);
...

// have the 2 parameters method call the(1 parameter) export'ed method
private void TouchUpInsideEvent (object sender, EventArgs e)
{
    TouchUpInsideEvent (sender as NSObject);
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM