How to use @selector in Xamarin.iOS

大城市里の小女人 提交于 2020-05-15 07:59:04

问题


Now I want to implement some functions in my xamarin.iOS projecs . I have found the solution but implemented in Objective-C.And it used the keyword @selector(...) . I found this issue as refer.However,as @theB said,use the dynamic type. Of course this means that type safety is only enforced at runtime, and you may incur some performance penalties.SO is there a better way to implement it? Any suggestion is welcome.Thanks!


回答1:


@selector is available in xamarin.iOS. It is included in the namespace ObjCRuntime.For example ,you can bind click action for a UIButton.Just like in Objective-C.

using ObjCRuntime;
. . .

UIButton button = new UIButton()
  {
    //. . .
  };
button.AddTarget(this, new Selector("ButtonClickAction:"),UIControlEvent.TouchUpInside);


[Export("ButtonClickAction:")]  
public void ButtonClickAction(UIButton sender)
{
  // . . .
}

[Export("ButtonClickAction:")]

It is important.Members with an [Export], making it possible for Objective-C to access them.For more details you can refer the docuemnt.



来源:https://stackoverflow.com/questions/52659151/how-to-use-selector-in-xamarin-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!