Introduction
Sometimes for security reasons, we need to restrict the copy, cut and paste clipboard options for Entry in our app. For example adding payee through Online Banking and while entering passwords. So, in this article we can learn how to disable those options for Entry using CustomRenderers concept in Xamarin.Forms.
In my previous article explained Copy and Paste using Clipboard in Xamarin.Forms.
- This article's source code is prepared by using Visual Studio. And it is better to install the latest Visual Studio updates from here.
- This article is prepared on a MAC machine.
- This sample project is Xamarin.Forms PCL project.
- This sample app is targeted for Android, iOS. And, tested for Android & iOS.
First, follow the below steps to create the new Xamarin.Forms project.
- Open Visual Studio for Mac.
- Click on the File menu and select New Solution.
- In the left pane of the dialog, let's select the type of templates to display. Multiplatform > App > Xamarin.Forms > Blank Forms App and click "Next".
- Next, enter your app name (Ex: DisableClipboardOperationsdemo). At the bottom, select target platforms to Android & iOS and shared code to Portable Class Library and click the "Next" button.
- Then, choose project location with the help of Browse button and click "Create".
- DisableClipboardOperationsdemo: It is for Shared Code
- DisableClipboardOperationsdemo.Droid: It is for Android.
- DisableClipboardOperationsdemo.iOS: It is for iOS.
Now, follow the below steps.
Portable Class Library (PCL)
Step 1:
In PCL, create a class name
HideClipboardEntry inside the CustomRenderers folder and it should inherit from Entry like below.
HideClipboardEntry.cs
- using Xamarin.Forms;
- namespace DisableClipboardOperationsdemo.CustomRenderers
- {
- public class HideClipboardEntry : Entry
- {
- }
- }
Step 2:
Create your own XAML page named HideClipboardPage.xaml inside the Views folder and make sure to refer to "HideClipboardEntry" class in XAML by declaring a namespace for its location and using the namespace prefix on the control element. The following code example shows how the "HideClipboardEntry" renderer class can be consumed by an XAML page:
HideClipboardPage.xaml
- <?xml version="1.0" encoding="UTF-8"?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:customCtrl="clr-namespace:DisableClipboardOperationsdemo.CustomRenderers"
- x:Class="DisableClipboardOperationsdemo.Views.HideClipboardPage"
- BackgroundColor="#533F95">
- <ContentPage.Content>
- <StackLayout HorizontalOptions="FillAndExpand"
- Padding="30">
- <Label Text="Disable Clipboard operations(Copy, Cut and Paste)"
- HorizontalTextAlignment="Center"
- HorizontalOptions="CenterAndExpand"
- TextColor="White" FontSize="20"/>
- <StackLayout VerticalOptions="CenterAndExpand"
- Spacing="15">
- <customCtrl:HideClipboardEntry
- Placeholder="Payee account number"
- PlaceholderColor="#533F95"
- HeightRequest="40"
- HorizontalOptions="FillAndExpand"
- BackgroundColor="White"/>
- <customCtrl:HideClipboardEntry
- Placeholder="Confirm payee account number"
- PlaceholderColor="#533F95"
- HeightRequest="40"
- HorizontalOptions="FillAndExpand"
- BackgroundColor="White"/>
- </StackLayout>
- </StackLayout>
- </ContentPage.Content>
- </ContentPage>
Note
The "customCtrl" namespace prefix can be named anything. However, the clr-namespace and assembly values must match the details of the custom renderer class. Once the namespace is declared the prefix is used to reference the custom control.
HideClipboardPage.xaml.cs
- using Xamarin.Forms;
- namespace DisableClipboardOperationsdemo.Views
- {
- public partial class HideClipboardPage : ContentPage
- {
- public HideClipboardPage()
- {
- InitializeComponent();
- }
- }
- }
Xamarin.Andriod
In an Android project, create a class and add the code like below
HideClipboardEntryRenderer.cs
- using Android.Content;
- using Android.Views;
- using DisableClipboardOperationsdemo.CustomRenderers;
- using DisableClipboardOperationsdemo.Droid;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.Android;
- [assembly: ExportRenderer(typeof(HideClipboardEntry), typeof(HideClipboardEntryRenderer))]
- namespace DisableClipboardOperationsdemo.Droid
- {
- public class HideClipboardEntryRenderer : EntryRenderer{
- protected override void OnElementChanged(ElementChangedEventArgs<Entry> e){
- base.OnElementChanged(e);
- if (Control != null){
- Control.CustomSelectionActionModeCallback = new Callback();
- Control.LongClickable = false;
- }
- }
- }
- 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;
- }
- }
- }
Xamarin.iOS
In iOS project, create a class and add the code like below
HideClipboardEntryRenderer.cs
- using DisableClipboardOperationsdemo.iOS;
- using UIKit;
- using Xamarin.Forms;
- using Xamarin.Forms.Platform.iOS;
- using ObjCRuntime;
- using Foundation;
- using DisableClipboardOperationsdemo.CustomRenderers;
- [assembly: ExportRenderer(typeof(HideClipboardEntry), typeof(HideClipboardEntryRenderer))]
- namespace DisableClipboardOperationsdemo.iOS
- {
- public class HideClipboardEntryRenderer : EntryRenderer
- {
- public override bool CanPerform(Selector action, NSObject withSender)
- {
- NSOperationQueue.MainQueue.AddOperation(() =>
- {
- UIMenuController.SharedMenuController.SetMenuVisible(false, false);
- });
- return base.CanPerform(action, withSender);
- }
- }
- }
Output:
Please download source code from here
No comments:
Post a Comment