Xamarin IOS CanPerformAction 不工作

Xamarin IOS CanPerformAction Not Working

我正在尝试禁用 Entry 中的复制和粘贴功能。

我使用继承自 EntryRenderer 的 CanPerform 动作。

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);
    }
}

您需要隐藏编辑菜单,使其不出现在 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);
    }
}

The singleton UIMenuController instance presents the menu interface for the Cut, Copy, Paste, Select, Select All, and Delete commands.

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