Copy And Paste Using Clipboard In Xamarin.Forms

Introduction

Copy and paste are used to share the data between apps, or within an app, and for almost all the platform-supported clipboard operations. So, in this article, we can learn how to achieve this functionality in Xamarin.Forms using DependencyServices concept.

Requirements

  • 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.

Description

Let's start

Step 1

First, follow the below steps to create a 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 on Next.
  • Next, enter your app name (Ex: ClipbordDemo). 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.

Now, the project structure will be created like below. 

  • ClipboardDemo
    It is for Shared Code

  • ClipboardDemo.Droid
    It is for Android.

  • ClipboardDemo.iOS
    It is for iOS

Portable Class Library(PCL)

Step 2

Create an interface IClipboardService inside the Interfaces folder and having method declaration of CopyToClipboard.

IClipboardService.cs

  1. namespace ClipboardDemo.Interfaces {  
  2.     public interface IClipboardService {  
  3.         void CopyToClipboard(string text);  
  4.     }  
  5. }  

Xamarin.Android

Create a class ClipboardService and you need to implement CopyToClipboard method like below.

ClipboardService.cs

  1. using System;  
  2. using ClipboardDemo.Interfaces;  
  3. using Xamarin.Forms;  
  4. using Android.Content;  
  5. using ClipboardDemo.Droid.DepencencyServices;  

  6. [assembly: Dependency(typeof(ClipboardService))]  
  7. namespace ClipboardDemo.Droid.DepencencyServices {  
  8.     public class ClipboardService: IClipboardService {  
  9.         public void CopyToClipboard(string text) {  
  10.             var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);  
  11.             ClipData clip = ClipData.NewPlainText("Android Clipboard", text);  
  12.             clipboardManager.PrimaryClip = clip;  
  13.         }  
  14.     }  
  15. }  

Note
ClipData's purpose is a representation of  clipped data on the clipboard. ClipData is a complex type containing one or more Item instances, each of which can hold one or more representations of an item of data. For displaying to the user, it also has a label.

Xamarin.iOS

In iOS also, we create a class ClipboardService and need to implement CopyToClipboard method like below.

ClipboardService.cs

  1. using ClipboardDemo.Interfaces;  
  2. using Xamarin.Forms;  
  3. using ClipboardDemo.iOS.Services; 
  4. using UIkit; 
  5. [assembly: Dependency(typeof(ClipboardService))]  
  6. namespace ClipboardDemo.iOS.Services {  
  7.     public class ClipboardService: IClipboardService {  
  8.         public void CopyToClipboard(string text) {  
  9.             UIPasteboard clipboard = UIPasteboard.General;  
  10.             clipboard.String = text;  
  11.         }  
  12.     }  
  13. }  

Note
UIPasteboard is an object that helps a user share data from one place to another within your app, and from your app to other apps.

PCL

Create your own Xaml page named VersionAndBuildNumberPage.xaml.

ClipboardDemoPage.xaml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
  4. xmlns:local="clr-namespace:ClipboardDemo" 
  5. BackgroundColor="White" 
  6. x:Class="ClipboardDemo.ClipboardDemoPage">  
  7. <StackLayout Padding="40, 60,40,0" 
  8. VerticalOptions="FillAndExpand" 
  9. HorizontalOptions="FillAndExpand">  
  10. <Label 
  11. Text="Copy and Paste" 
  12. FontSize="25" 
  13. FontAttributes="Bold" 
  14. TextColor="#533F95" 
  15. HorizontalOptions="CenterAndExpand" />  
  16. <StackLayout 
  17. VerticalOptions="CenterAndExpand" 
  18. Spacing="40">
  19. <Entry 
  20. Placeholder="Enter text" 
  21. PlaceholderColor="#533F95" 
  22. TextColor="#533F95" 
  23. x:Name="ClipboardCopyText" 
  24. HorizontalOptions="FillAndExpand" 
  25. HeightRequest="45" 
  26. VerticalOptions="StartAndExpand" />  
  27. <Button 
  28. Text="Copy To Clipboard" 
  29. BackgroundColor="#533F95" 
  30. TextColor="White" 
  31. HorizontalOptions="FillAndExpand" 
  32. HeightRequest="45" 
  33. Clicked="Btn_CopyToClipboard_Click" /> 
  34. </StackLayout>  
  35. </StackLayout>  
  36. </ContentPage>  

Call to DependencyService

Now, let us call the dependency service in the code behind under CopyToClipboard click event for copying the text to clipboard.

  1. var clipboardService = DependencyService.Get<IClipboardService>();  
  2. clipboardService?.CopyToClipboard(ClipboardCopyText.Text);  

ClipboardDemoPage.xaml.cs

  1. using System;  
  2. using ClipboardDemo.Interfaces;  
  3. using Xamarin.Forms;  
  4. namespace ClipboardDemo {  
  5.     public partial class ClipboardDemoPage: ContentPage {  
  6.         public ClipboardDemoPage() {  
  7.             InitializeComponent();  
  8.         }  
  9.         private void Btn_CopyToClipboard_Click(object sender, EventArgs e) {  
  10.             if (!string.IsNullOrEmpty(ClipboardCopyText.Text)) {  
  11.                 var clipboardService = DependencyService.Get < IClipboardService > ();  
  12.                 clipboardService ? .CopyToClipboard(ClipboardCopyText.Text);  
  13.                 DisplayAlert("ClipboardDemo""Text copied!""Ok");  
  14.             } else {  
  15.                 DisplayAlert("ClipboardDemo""Please enter the text""Ok");  
  16.             }  
  17.         }  
  18.     }  
  19. }  

Note
In the above class, we are calling Clipboard Service and passing user entered text in ClipboardCopyText Entry.

Output

                    

 Please download the source code from here.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now