Windows 10 Step by Step Tutorial – Copy and Paste Functionality

Introduction

There are various ways in which you can exchange data between apps in Windows Apps. Some of the techniques include

  • Copy and Paste
  • Share Contract
  • Drag and Drop

Most of the default controls that are available in XAML supports the basic clipboard operations but there are times when you might want to implement the copy and paste programmatically.

This tutorial introduces you to the new functionality in Windows Universal App (UWP) which lets the developers to integrate the clipboard operations in their apps.

Tutorial – How to integrate Copy and Paste functionality in UWP apps?

Integrating the Copy and paste functionality in a UWP app is a four step process

1. Add the namespace “Windows.ApplicationModel.DataTransfer” to the code behind file. Create an instance of the DataPackage class and specify the RequestedOperation property to the desired functionality.

DataPackage dataPackageobj = new DataPackage 
{ 

   RequestedOperation = DataPackageOperation.Copy 

}; 

The possible values that the RequestedOperation can take are

  • DataPackageOperation.Copy
  • DataPackageOperation.Move
  • DataPackageOperation.Link
  • DataPackageOperation.None

2. Use the SetText method of the DataPackage instance to set the text that you want to copy or cut programmatically.

// Set the Text that you want to copy 

dataPackageobj.SetText("Welcome to Windows 10 Tutorials"); 

3. Assign the datapackage instance to the Clipboard.SetContent method.

// Set the datapackage instance to the Clipboard 

Clipboard.SetContent(dataPackageobj); 

This ensures that the content is now copied to the clipboard.

The complete function to copy the value to the clipboard is below.

private static void CopyToClipboard() 
{ 

   // Create an instance of the DataPackage and set the RequestedOperation to DataPackageOperation.Copy 

   DataPackage dataPackageobj = new DataPackage 
   { 

      RequestedOperation = DataPackageOperation.Copy 

   }; 

   // Set the Text that you want to copy 

   dataPackageobj.SetText("Welcome to Windows 10 Tutorials"); 

   // Set the datapackage instance to the Clipboard 

   Clipboard.SetContent(dataPackageobj); 

} 

How to Paste the Content from the Clipboard programmatically within your App?

You can retrieve the content of the clipboard by following the below steps.

1. Use the Clipboard.GetContent method which returns the DataPackageView.

DataPackageView dataPackageobj = Clipboard.GetContent(); 

2. Copy the content from the dataPackageobj using the GetTextAsync() method for the text values. This returns the string from the clipboard.

string output = await dataPackageobj.GetTextAsync(); 

Below is the complete method to get the string from the clipboard and display it in a message box.

private static async void PasteContent() 
{ 

   DataPackageView dataPackageobj = Clipboard.GetContent(); 

   string output = await dataPackageobj.GetTextAsync(); 

   // Display the output on a MessageBox 

   MessageDialog dialog = new MessageDialog(output); 

   dialog.ShowAsync(); 

} 


clip_image002

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...