在Xamarin表单输入字段(即输入项)上禁用复制/粘贴 [英] Disable copy/paste on Xamarin forms input field i.e. Entry

查看:173
本文介绍了在Xamarin表单输入字段(即输入项)上禁用复制/粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在禁用xamarin表单条目上的复制/粘贴选项菜单,我可以使用 IsPassword = true 属性禁用复制选项,但是此属性还将正常输入字段转换为密码字段,这不是必需的.

I am working on disabling copy/paste option menus on xamarin forms Entry, I am able to disable copy option using IsPassword=true attribute but this attribute also converts the normal input field to password field, which is not a requirement.

<Entry IsPassword="true" Placeholder="Password" TextColor="Green" BackgroundColor="#2c3e50" />

谢谢.

推荐答案

这与Forms的功能有关.这里以iOS为例,另一个答案的Bugzilla问题中提到的CanPerform替代使用的是UIMenuController作为withSender,而不是UITextField本身,否则可能会出现这种情况.这是因为EntryRenderer类是ViewRenderer<TView, TNativeView>类型,并且随后正在使用其CanPerform中包含的任何TNativeView(在本例中为UITextView).由于默认情况下不会覆盖任何内容,因此仍然可以看到UIMenuController中的所有剪切/复制/粘贴选项.

This has to do with how Forms functions. Using iOS as the example here, the CanPerform override referred to in the other answer's Bugzilla issue is using the UIMenuController as the withSender and not the UITextField itself that might otherwise be expected. This is because the EntryRenderer class is a ViewRenderer<TView, TNativeView> type and subsequently is using whatever TNativeView (in this case, the UITextView) has in its CanPerform. Because nothing is going to be overridden by default, one still sees all of the cut/copy/paste options in the UIMenuController.

因此,会有两个选择.您可以首先进行修改,如果您不想复制/粘贴但可以摆脱其他所有问题,则可以在继承自EntryRenderer的自定义渲染器中使用UIMenuController.SharedMenuController.SetMenuVisible(false, false).如果您环顾四周,那么可能存在的路线也存在类似的问题.

As a result, there would be a couple options. You could first make the modification where if you don't want copy/paste but are fine with getting rid of everything else, you can use UIMenuController.SharedMenuController.SetMenuVisible(false, false) in a custom renderer inheriting from EntryRenderer. If you look around on SO, there are similar questions where this is a possible route.

或者,您可以创建一个从ViewRenderer<TView, TNativeView>继承为ViewRenderer<Entry, YourNoCopyPasteUITextFieldClassName>的"true"自定义渲染器.然后,从UITextField继承的类可以重写CanPerform,如下所示:

Alternatively, you can create a "true" custom renderer inheriting from ViewRenderer<TView, TNativeView> as ViewRenderer<Entry, YourNoCopyPasteUITextFieldClassName>. The class inheriting from UITextField can then override CanPerform as something like follows:

    public override bool CanPerform(Selector action, NSObject withSender)
    {
        if(action.Name == "paste:" || action.Name == "copy:" || action.Name == "cut:")
            return false;

        return base.CanPerform(action, withSender);
    }

这将需要更多的精力,因为自定义渲染器的行为与EntryRenderer并不相同,但是由于Xamarin.Forms现在是开源的,您可以向其寻求有关EntryRenderer正常运行方式的一些想法.对于Android,可能需要做类似的事情.

This will require more effort because the custom renderer will not have the same behavior as the EntryRenderer, but as Xamarin.Forms is now open source, you could look to it for some ideas as to how the EntryRenderer functions normally. Something similar would likely have to be done for Android.

编辑:对于Android,您可以使用以下SO答案作为起点:

Edit: For Android, you can probably use this SO answer as a starting point: How to disable copy/paste from/to EditText

另一个自定义渲染器,这次是从ViewRenderer<Entry, EditText>继承,并在其中创建一个类,如下所示(以最基本的形式):

Another custom renderer, this time inheriting from ViewRenderer<Entry, EditText>, and create a class inside of it like this (in the most basic form):

class Callback : Java.Lang.Object, ActionMode.ICallback
{

    public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
    {
        return false;
    }

    public bool OnCreateActionMode(ActionMode mode, IMenu menu)
    {
        return false;
    }

    public void OnDestroyActionMode(ActionMode mode)
    {

    }

    public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
    {
        return false;
    }
}

然后,在您的OnElementChanged方法中,您可以设置本机控件和CustomSelectionActionModeCallback值:

Then, in your OnElementChanged method, you can set the native control and the CustomSelectionActionModeCallback value:

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

        if (Control != null)
        {
            Control.CustomSelectionActionModeCallback = new Callback();
        }
    }

执行以下操作似乎会在工具栏上禁用自定义条目上的所有复制/粘贴/剪切功能.但是,您仍然可以长按一下以显示粘贴"按钮,除了将LongClickable设置为false之外,我对此一直没有找到答案.如果在这方面我发现其他任何内容,请确保进行更新.

Doing something like the following appears to disable all of the copy/paste/cut functionality on the custom entry as far as the toolbar goes. However, you can still long click to show the paste button, to which I've poked around a bit hadn't found an answer yet beyond setting LongClickable to false. If I do find anything else in that regard, I'd make sure to update this.

这篇关于在Xamarin表单输入字段(即输入项)上禁用复制/粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆