If I paste something in an Entry, it doesn't fire Trigger

EnricoRossiniEnricoRossini USMember ✭✭✭✭
edited October 2017 in Xamarin.Forms

Hi guys,
I have an Entry with some Triggers. One of them is checking is an email is valid. The problem is that a user pastes an email, the Entry doesn't fire the Triggers.

How can I update an Entry in this case?

I tried to prevent Cut, Paste or Copy but it doesn't work.

iOS

[assembly: ExportRenderer(typeof(Entry), typeof(EntryCustomRenderer))]
namespace MyProject.iOS.Renderers
{
    /// <summary> 
    /// Entry extra renderer. 
    /// </summary> 
    public class EntryCustomRenderer : EntryRenderer
    {
        [...]

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

            return base.CanPerform(action, withSender);
        }
    }
}

Android

Callback

namespace MyProject.Droid.Renderers
{
    public 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;
        }
    }
}

EntryRender

public class EntryCustomRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        [...]

        // prevent copy/paste
        Control.CustomSelectionActionModeCallback = new Callback();
    }
}

Thanks in advance

Tagged:

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @EnricoRossini - I remove Cut and Paste options using the following (excuse the magic numbers):

    For iOS:

        public override bool CanPerform(ObjCRuntime.Selector action, NSObject withSender)
        {
            if ((action == new ObjCRuntime.Selector("paste:"))
                || (action == new ObjCRuntime.Selector("cut:")))
            {
                return false;
            }
            else
                return base.CanPerform(action, withSender);
        }
    

    For Android:

        public bool OnCreateActionMode(ActionMode mode, IMenu menu)
        {
            // Cut - 16908320
            // Copy - 16908321
            // Paste - 16908322
            // Share - 16908341
    
            if (menu != null)
            {
                menu.RemoveItem(16908320); // cut - 1020020 hex
                menu.RemoveItem(16908322); // paste - 1020022 hex
            }
    
            return true;
        }
    
  • EnricoRossiniEnricoRossini USMember ✭✭✭✭

    Thank @JohnHardman
    I applied your code but I have the same result. See the attach. I replaced my code in Callback with yours.

  • JohnHardmanJohnHardman GBUniversity mod
    edited October 2017

    @EnricoRossini - I cannot remember why (I should have commented it), but for Android, I put the callback handling in my main Activity class, and then in my custom Entry renderer, did:

                FormsEditText nativeEntryEditText = Control;
                nativeEntryEditText.CustomSelectionActionModeCallback = MyAppAndroid.MainActivity.TheInstance;
    

    That doesn't look like something I would have done without a good reason :-) Might be worth a try.

  • EnricoRossiniEnricoRossini USMember ✭✭✭✭
    edited October 2017

    In my MainActivity I put the callback

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public static UI.Forms.App formsApp;
    
        protected override void OnCreate(Bundle bundle)
        {
            [...]
        }
    
        public class MyCallback : Java.Lang.Object, ActionMode.ICallback
        {
            public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
            {
                return false;
            }
    
            public bool OnCreateActionMode(ActionMode mode, IMenu menu)
            {
                // Cut - 16908320
                // Copy - 16908321
                // Paste - 16908322
                // Share - 16908341
    
                if (menu != null)
                {
                    menu.RemoveItem(16908320); // cut - 1020020 hex
                    menu.RemoveItem(16908322); // paste - 1020022 hex
                }
    
                return true;
            }
    
            public void OnDestroyActionMode(ActionMode mode)
            {
            }
    
            public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
            {
                return false;
            }
        }
    }
    

    In my EntryCustomRenderer I wrote

    public class EntryCustomRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
    
            if (Control != null)
            {
                [...]
    
                EntryEditText nativeEntryEditText = Control;
                nativeEntryEditText.CustomSelectionActionModeCallback = new MainActivity.MyCallback();
            }
        }
    
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
        }
    }
    

    I can paste everything.

  • JohnHardmanJohnHardman GBUniversity mod

    @EnricoRossini - I'm afraid I don't remember what more (if anything) I had to do on Android to remove Cut & Paste. Whilst I have it working, I don't have time right now to work out what else in my codebase might have affected this.

    Is the other code above working for you on iOS?

  • EnricoRossiniEnricoRossini USMember ✭✭✭✭

    In iOS action has a lot of Name

    cut:
    copy:
    select:
    selectAll:
    delete:
    _promptForReplace:
    _transliterateChinese:
    _insertDrawing:
    _showTextStyleOptions:
    _lookup:
    _define:
    _addShortcut:
    _accessibilitySpeak:
    _accessibilitySpeakLanguageSelection:
    _accessibilityPauseSpeaking:
    _share:
    makeTextWritingDirectionRightToLeft:
    makeTextWritingDirectionLeftToRight:

    But paste is not in the list. Then when an entry has a long press, it displays Paste

  • SivaShankarArumugamSivaShankarArumugam USMember ✭✭✭

    @EnricoRossini did you find any solution to disable paste function

  • noob8129noob8129 Member ✭✭

    @EnricoRossini @SivaShankarArumugam Were you guys able to hide paste? I'm having this issue now.

Sign In or Register to comment.