8003

I need to block the paste function in a datepicker on XamarinForms

Question:

In the solution that I'm building, I have a date-picker that works fine. But, if the user press the touch and paste something, it will overwrite the date.

When the user pastes, the events of OnElementChanged, OnElementPropertyChanged and even the INotifyPropertyChanged are not raised. I also can't find a option to block the paste function. (When the date is choose normally, the events are being called.)

This happens in iOS and Android. Can someone help me? I am stuck and my researches don't help me much.

Answer1:

<strong>On iOS platform</strong>

DatePicker behaves this way because under the hood it is implemented via UITextField. To avoid editing of UITextField you have to assign it a delegate which returns false from ShouldChangeCharacters method.

The full solution will be the following:

Declare a child of a DatePicker in a shared project:

public class ExtendedDatePicker : DatePicker { }

Declare a corresponding renderer in iOS project:

[assembly: ExportRenderer(typeof(ExtendedDatePicker), typeof(ExtendedDatePickerRenderer))] ... public class ExtendedDatePickerRenderer : DatePickerRenderer { private UneditableUITextFieldDelegate _delegate = new UneditableUITextFieldDelegate(); protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e) { base.OnElementChanged(e); Control.Delegate = _delegate; } }

Implement the UneditableUITextFieldDelegate:

public class UneditableUITextFieldDelegate : NSObject, IUITextFieldDelegate { [Export("textField:shouldChangeCharactersInRange:replacementString:")] public bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString) => false; }

This will not prevent the 'copy'/'paste' menu from being shown but DatePicker value will not be changed after pressing them.

<strong>On Android platform</strong>

The underlying UI element for DatePicker on Android is EditText.

I have posted articles explaining how to manipulate context menu of EditText in <a href="https://medium.com/@anna.domashych/selectable-read-only-multiline-text-field-on-android-169c27c55408" rel="nofollow">native Android</a> and <a href="https://medium.com/@anna.domashych/selectable-read-only-multiline-text-field-in-xamarin-forms-69d09276d580" rel="nofollow">Xamarin.Forms</a>. You can get the overall idea there.

I was not able to perform a long tap on a DatePicker to see the problem thus I can only guess about the proper fix, but a renderer similar to this one should completely disable context menu for selected text as well as a paste option when a user taps a cursor:

[assembly: ExportRenderer(typeof(ExtendedDatePicker), typeof(ExtendedDatePickerRenderer))] ... public class ExtendedDatePickerRenderer : DatePickerRenderer { public ExtendedDatePickerRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e) { base.OnElementChanged(e); Control.CustomSelectionActionModeCallback = new CustomSelectionActionModeCallback(); Control.CustomInsertionActionModeCallback = new CustomInsertionActionModeCallback(); } }

CustomInsertionActionModeCallback and CustomInsertionActionModeCallback would return false from OnCreateActionMode and will prevent menu from appearing.

public class CustomInsertionActionModeCallback : Java.Lang.Object, ActionMode.ICallback { public bool OnCreateActionMode(ActionMode mode, IMenu menu) => false; public bool OnActionItemClicked(ActionMode m, IMenuItem i) => false; public bool OnPrepareActionMode(ActionMode mode, IMenu menu) => true; public void OnDestroyActionMode(ActionMode mode) { } } public class CustomSelectionActionModeCallback : Java.Lang.Object, ActionMode.ICallback { public bool OnActionItemClicked(ActionMode m, IMenuItem i) => false; public bool OnCreateActionMode(ActionMode mode, IMenu menu) => false; public bool OnPrepareActionMode(ActionMode mode, IMenu menu) => true; public void OnDestroyActionMode(ActionMode mode) { } }

<strong>=======</strong><br /><strong>UPDATE:</strong> this question inspired me to create an <a href="https://medium.com/@anna.domashych/xamarin-forms-disabling-cut-paste-menu-on-datepicker-e2dc96298a8f" rel="nofollow">article</a> extending this answer with some explanations and details.

Recommend

  • How to add an icon in navigation bar for navigation page in xamarin forms for android?
  • UISlider with increased slider size but no thumb distortion in iOS App
  • Xamarin forms Android how We change Tabbed Page Icon Size
  • Date format jqplot categoryaxisrenderer after ticks add
  • Is it possible / sensible to use external templating engine like dust.js or any other with anjularjs
  • Overriding createToolTip() in a custom CellRenderer
  • PHP mysql_real_escape_string() and % character
  • moving particles on face of imported *.obj in threejs
  • Vertical lines at X Axis iOS-charts
  • Grid column filter not working with two models in magento custom module
  • untrusted HTML in d3.js v4 and AngularJS
  • DataTables+RequireJS: Cannot read property 'defaults' of undefined
  • Resizing ToolStripButtons to fit complete BackGround image
  • Linear gradient not applying in Webkit with d3 generated SVG
  • Wrap assemblies in .NET?
  • Why can't I use non-integral types with switch [duplicate]
  • Extending the Django 1.11 User Model
  • Less Conflicting Session Manager for Zope 2
  • Translating C# to PowerShell in InterIMAP
  • Django Haystack Rebuild Index
  • Does Apportable support to build library binary (.a/.so)?
  • How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View?
  • Ubuntu and bcrypt
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • x64 applications using gdi+: what are the consequences on performance?
  • Switching to Release Build causes runtime error in Web Reference
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Web-crawler for facebook in python
  • QuartzCore.framework for Mono Develop
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • C# - Getting references of reference
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?