[英]How to fire TouchUpInside of UIButton Programmatically in xamarin ios C#
我試圖以編程方式觸發Uibutton的touchupinside事件。 我怎樣才能做到這一點 ? 我嘗試的代碼使用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. "
我希望實現類似的目標
UIButton.FireEvent("TouchUpInsideEvent") - this will fire the TouchUpInsideEvent
UIButton.PerformSelector(new MonoTouch.ObjCRuntime.Selector ("TouchUpInsideEvent:"), null, 2000f);
這是代碼
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);
}
下面的代碼片段就足夠了
btnObj.SendActionForControlEvents(UIControlEvent.TouchUpInside);
它必須從主線程調用
上面有一些不同的事情。
首先, MT4117
是正確的。 這是因為你的[Export]
屬性指定一個選擇到只有一個參數的方法(即它只有一個:
),而管理的方法有兩個參數(這是.NET事件默認值)。 注冊服務商將發現此類情況並報告錯誤。
其次, PerformSelector
方法是對performSelector:...
選擇器的綁定(大多數是在NSObject
協議上定義的,而不是類)。 因此,它們具有相同的限制(例如,它們可以處理的參數數量)。
第三,有幾種方法可以調用您自己的代碼。 就像@jonathanpeppers建議的那樣,一個簡單的方法就是在需要時直接調用托管方法。
另一個是調整代碼以匹配1和2要求,例如
// 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.