How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

The Clipboard class is available at Xamarin.Essentials API. It is used to copy and paste the text to the system clipboard between the applications. Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# languages, leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.

Reading this article, you will learn how to copy and paste the text using Clipboard in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross platform application development.

The following important tools are required for developing Xamarin Forms App,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. Android API 19 or higher and UWP 10.0.16299.0 or higher.

Now we can discuss step by step app development.

Step 1

Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give a suitable name for your app (XamFormClipboard) ->OK

How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 2

Select the Cross Platform template as Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standard. Afterwards, Visual Studio creates 3 projects (Portable, Droid, UWP).

How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 3

For adding reference,

Right click your solution (XamFormClipboard) and select Manage Nuget Packages.

For adding a Xamarin.Essentials reference, choose browse and search Xamarin.Essentials. Select the package and select all the projects (portable, Android, UWP) and install it.

How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 4

Add the Label and Entry controls in Mainpage.Xaml for displaying the title and sample text and paste the clipboard content.

  1. <Label FontAttributes="Bold" Text="Copy and Paste the text using Clipboard in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  2. <Label HorizontalOptions="Center" Text="This is the clipboard test content" x:Name="lblclip"></Label>  
  3. <Entry Placeholder="Paste your clipboard content here" />  

Step 5

Add the following code in the MainActivity.cs in XamFormClipboard.Android project,

In onCreate() method,

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState); 

 

Also add the below method,

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {  
  2.     Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  3.     base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  

 

Step 6

Add the following namespace and code in Mainpage.Xaml.cs

  1. using Xamarin.Essentials;  
  2. var tapGestureRecognizer = new TapGestureRecognizer();  
  3. tapGestureRecognizer.Tapped += async (s, e) => {  
  4.     Clipboard.SetText(lblclip.Text);  
  5.     if (Clipboard.HasText) {  
  6.         var text = await Clipboard.GetTextAsync();  
  7.         await DisplayAlert("Success", string.Format("Clipboard content is ({0})", text), "OK");  
  8.     }  
  9. };  
  10. lblclip.GestureRecognizers.Add(tapGestureRecognizer);  

 

Note
Automatically the following code will be generated in XAML code view, while we are finished in the design view.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormClip" x:Class="XamFormClip.MainPage">  
  3. <StackLayout>  
  4.    <Label FontAttributes="Bold" Text="Copy and Paste the text using Clipboard in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>  
  5.    <Label HorizontalOptions="Center" Text="This is the clipboard test content" x:Name="lblclip"></Label>  
  6.    <Entry Placeholder="Paste your clipboard content here" />  
  7. </StackLayout>  
  8. </ContentPage>  
Step 7

We will test Android and UWP. So, we can set the Multiple Startup Projects as XamFormClipboard.Android and XamFormClipboard.UWP (universal Windows)

How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 8

Deploy your app in UWP and Android. After tapping the copying content, the output of the XamFormClipboard app is:

 
How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
After pasting the content in the Entry control:
 
How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
How To Use Clipboard In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Summary

Now you have successfully verified the Clipboard copy and text in Xamarin Forms applications using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.


Similar Articles