iOS Xamarin.iOS 中的选择器
iOS selector in Xamarin.iOS
我正在尝试在 Xamarin.iOS 中创建一个 UIAccessibilityCustomAction
。此方法需要 name, target, selector
作为参数(如您所见 here)。问题出在 selector
参数上。
在Xcode中(使用Swift)我可以像这样轻松实现它:
let up = UIAccessibilityCustomAction(name: "Increment", target: self, selector: #selector(increment))
@objc private func increment() -> Bool{
//selector implementation
}
在 Xamarin(使用 C#)中我试过:
UIAccessibilityCustomAction up = new UIAccessibilityCustomAction(name: "Increment", target: iospage, selector: new Selector("Increment"));
据说 Selector
可以将 String
或 IntPtr
作为参数。因为我不知道 IntPtr
是什么以及我应该如何使用它,所以我尝试使用 String
参数,正如您在上面看到的,并且我尝试像这样实现选择器,遵循 .
[Export("Increment")]
private void Increment()
{
//selector implementation
}
问题是这个方法似乎从未被调用过(我试图让它在调用 UIAccessibilityCustomAction 时记录一些东西,但没有显示日志),可能是因为它是错误的实现方式。
有什么想法吗?
谢谢
UIAccessibilityCustomAction 有另一个实例化方法,您可以将自定义操作传递给它。
UIAccessibilityCustomAction c = new UIAccessibilityCustomAction("Increment",
(UIAccessibilityCustomAction customAction) =>
{
//selector implementation
});
感谢@Cole Xia - MSFT 的提示,我找到了另一种似乎更容易处理的实例化方法,仅使用 name, actionHandler
,因此没有 target
和 selector
. actionHandler
更容易使用,因为它只是一个需要 bool return 类型的函数。
我的实现:
UIAccessibilityCustomAction up = new UIAccessibilityCustomAction("Increment", actionHandler: Increment);
private bool Increment(UIAccessibilityCustomAction customAction)
{
//implementation of the handler
}
我正在尝试在 Xamarin.iOS 中创建一个 UIAccessibilityCustomAction
。此方法需要 name, target, selector
作为参数(如您所见 here)。问题出在 selector
参数上。
在Xcode中(使用Swift)我可以像这样轻松实现它:
let up = UIAccessibilityCustomAction(name: "Increment", target: self, selector: #selector(increment))
@objc private func increment() -> Bool{
//selector implementation
}
在 Xamarin(使用 C#)中我试过:
UIAccessibilityCustomAction up = new UIAccessibilityCustomAction(name: "Increment", target: iospage, selector: new Selector("Increment"));
据说 Selector
可以将 String
或 IntPtr
作为参数。因为我不知道 IntPtr
是什么以及我应该如何使用它,所以我尝试使用 String
参数,正如您在上面看到的,并且我尝试像这样实现选择器,遵循
[Export("Increment")]
private void Increment()
{
//selector implementation
}
问题是这个方法似乎从未被调用过(我试图让它在调用 UIAccessibilityCustomAction 时记录一些东西,但没有显示日志),可能是因为它是错误的实现方式。
有什么想法吗?
谢谢
UIAccessibilityCustomAction 有另一个实例化方法,您可以将自定义操作传递给它。
UIAccessibilityCustomAction c = new UIAccessibilityCustomAction("Increment",
(UIAccessibilityCustomAction customAction) =>
{
//selector implementation
});
感谢@Cole Xia - MSFT 的提示,我找到了另一种似乎更容易处理的实例化方法,仅使用 name, actionHandler
,因此没有 target
和 selector
. actionHandler
更容易使用,因为它只是一个需要 bool return 类型的函数。
我的实现:
UIAccessibilityCustomAction up = new UIAccessibilityCustomAction("Increment", actionHandler: Increment);
private bool Increment(UIAccessibilityCustomAction customAction)
{
//implementation of the handler
}