Xamarin IOS CanPerformAction不工作

时间:2016-12-16 06:34:12

标签: ios xamarin copy xamarin.forms paste

我试图在Entry中禁用复制和粘贴功能。

我使用CanPerform动作继承自EntryRenderer。

public class NEntryRenderer : EntryRenderer
{
    public override bool CanPerform(Selector action, NSObject withSender)
    {
        if (action == new Selector("paste:") || action == new Selector("copy:"))
            return false;

        return base.CanPerform(action, withSender);
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
    }
}

1 个答案:

答案 0 :(得分:2)

您需要隐藏编辑菜单才能显示在UITextField上:

public class NEntryRenderer : EntryRenderer
{
    public override bool CanPerform(Selector action, NSObject withSender)
    {
        NSOperationQueue.MainQueue.AddOperation(() => 
        {
            UIMenuController.SharedMenuController.SetMenuVisible(false, false);
        });
        return base.CanPerform(action, withSender);
    }
}
  

单例UIMenuController实例显示剪切,复制,粘贴,选择,全选和删除命令的菜单界面。

参考:https://developer.apple.com/reference/uikit/uimenucontroller