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
- namespace ClipboardDemo.Interfaces {
- public interface IClipboardService {
- void CopyToClipboard(string text);
- }
- }
Xamarin.Android
Create a class ClipboardService and you need to implement CopyToClipboard method like below.
ClipboardService.cs
- using System;
- using ClipboardDemo.Interfaces;
- using Xamarin.Forms;
- using Android.Content;
- using ClipboardDemo.Droid.DepencencyServices;
- [assembly: Dependency(typeof(ClipboardService))]
- namespace ClipboardDemo.Droid.DepencencyServices {
- public class ClipboardService: IClipboardService {
- public void CopyToClipboard(string text) {
- var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
- ClipData clip = ClipData.NewPlainText("Android Clipboard", text);
- clipboardManager.PrimaryClip = clip;
- }
- }
- }
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
- using ClipboardDemo.Interfaces;
- using Xamarin.Forms;
- using ClipboardDemo.iOS.Services;
- using UIkit;
- [assembly: Dependency(typeof(ClipboardService))]
- namespace ClipboardDemo.iOS.Services {
- public class ClipboardService: IClipboardService {
- public void CopyToClipboard(string text) {
- UIPasteboard clipboard = UIPasteboard.General;
- clipboard.String = text;
- }
- }
- }



Join the conversation! Your thoughts help the community grow.