简体   繁体   English

Xamarin.iOS 中的 iOS 选择器

[英]iOS selector in Xamarin.iOS

I am trying to create an UIAccessibilityCustomAction in Xamarin.iOS.我正在尝试在 Xamarin.iOS 中创建一个UIAccessibilityCustomAction This method require name, target, selector as arguments (as you can see here ).此方法需要name, target, selector为 arguments(如您在此处看到的)。 The problem is with the selector argument.问题在于selector参数。

In Xcode (using Swift) I can easily implement it like this:在 Xcode(使用 Swift)中,我可以像这样轻松实现它:

let up = UIAccessibilityCustomAction(name: "Increment", target: self, selector: #selector(increment))


@objc private func increment() -> Bool{
    //selector implementation
}

In Xamarin (using C#) I tried that:在 Xamarin(使用 C#)中,我尝试过:

UIAccessibilityCustomAction up = new UIAccessibilityCustomAction(name: "Increment", target: iospage, selector: new Selector("Increment"));

And its being said that Selector can take both a String or a IntPtr as argument.据说Selector可以将StringIntPtr作为参数。 Since I have no idea what IntPtr is and how should I use it, I tried to use the String parameter, as you can see above, and I tried to implement the selector like this, following this answer .由于我不知道IntPtr是什么以及我应该如何使用它,所以我尝试使用String参数,如您在上面看到的,并且我尝试按照这个答案实现这样的选择器。

[Export("Increment")]
    private void Increment()
    {
        //selector implementation
    }

Problem is that seems like this method never got called (i tried to make it log something when the UIAccessibilityCustomAction is called, but no log is shown), probably because it is the wrong way to implement it.问题是这个方法似乎从未被调用(我试图在调用 UIAccessibilityCustomAction 时让它记录一些东西,但没有显示日志),可能是因为它是错误的实现方式。

Any idea?任何想法?

Thanks谢谢

UIAccessibilityCustomAction has another instantiate method which you can pass an custom action into it. UIAccessibilityCustomAction 有另一个实例化方法,您可以将自定义操作传递给它。

 UIAccessibilityCustomAction c = new UIAccessibilityCustomAction("Increment", 
        (UIAccessibilityCustomAction customAction) =>
        {

               //selector implementation
        });

Thanks to the tip of @Cole Xia - MSFT, I found another instantiate method that seems to be easier to handle, using only name, actionHandler , so without a target and selector .感谢@Cole Xia - MSFT 的提示,我发现了另一个似乎更容易处理的实例化方法,只使用name, actionHandler ,因此没有targetselector actionHandler is easier to use, cause it's only a function that needs a bool return type. actionHandler更易于使用,因为它只有 function 需要 bool 返回类型。

My implementation:我的实现:

UIAccessibilityCustomAction up = new UIAccessibilityCustomAction("Increment", actionHandler: Increment);

private bool Increment(UIAccessibilityCustomAction customAction)
{
    //implementation of the handler
}

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

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