Copy And Paste Text Content Using Clipboard In UWP With XAML And C#

Reading this article, you will learn how to copy and paste the text content using clipboard in Universal Windows Apps development with XAML and Visual C#.

Copy and paste is the classic method to exchange data between apps, or within an app, and almost all the apps can support clipboard operations to some degree.

Reading this article, you will learn how to perform copy and paste operations on the text content using the clipboard in Universal Windows Apps development with XAML and Visual C#.

The following important tools are required for developing the UWP.

Step 1

Open Visual studio 2017 and go to Start -> New Project -> select Windows Universal (under Visual C#) -> Blank App(Universal Windows) -> give it a suitable name (UWPTxtCopyPaste) -> OK.

 

After choosing the Target and Minimum platform version that your Windows Universal Application will support, the Project creates App.xaml and MainPage.xaml.

 

Step 2

Add the following controls in design window for performing the text content copy and paste activity. Add the TextBox, TextBlock, and Button Controls and change their name and text properties.

 

Step 3

Now, add the following namespace and code in the Mainpage.xaml.cs.

  1. using Windows.ApplicationModel.DataTransfer;  
  2. private void BtnCopy_Click(object sender, RoutedEventArgs e) {  
  3.     var dataPackage = new DataPackage();  
  4.     dataPackage.SetText(txtCopy.Text);  
  5.     Windows.ApplicationModel.DataTransfer.Clipboard.SetContent(dataPackage);  
  6. }  
  7. private async void BtnPaste_Click(object sender, RoutedEventArgs e) {  
  8.     var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();  
  9.     if (dataPackageView.Contains(StandardDataFormats.Text)) {  
  10.         var text = await dataPackageView.GetTextAsync();  
  11.         TblPaste.Text = Environment.NewLine + text;  
  12.     } else {  
  13.         TblPaste.Text = "Text: " + Environment.NewLine + "Text format is not available in clipboard";  
  14.     }  
  15. }  

Automatically, the following code will be generated in XAML code view, while we are done in the design view.

  1. <Page x:Class="UWPTxtCopyPaste.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPTxtCopyPaste" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="90,100,0,0" Text="Text content copy and paste using clipboard in UWP With XAML and C#" TextWrapping="Wrap" VerticalAlignment="Top" FontWeight="Bold" FontSize="14" />  
  4.         <TextBlock x:Name="TblCopy" HorizontalAlignment="Left" Margin="108,150,0,0" Text="Type your Content to be copy" TextWrapping="Wrap" VerticalAlignment="Top" />  
  5.         <TextBox x:Name="txtCopy" HorizontalAlignment="Left" Margin="111,190,0,0" Text="" VerticalAlignment="Top" Height="103" Width="215" TextWrapping="Wrap" />  
  6.         <Button x:Name="BtnCopy" Content="Copy" HorizontalAlignment="Left" Margin="119,326,0,0" VerticalAlignment="Top" Click="BtnCopy_Click" />  
  7.         <Button x:Name="BtnPaste" Content="Paste" HorizontalAlignment="Left" Margin="410,331,0,0" VerticalAlignment="Top" Click="BtnPaste_Click" />  
  8.         <TextBlock x:Name="TblPaste" HorizontalAlignment="Left" Height="95" Margin="375,196,0,0" Text="Awaiting for Paste" TextWrapping="Wrap" VerticalAlignment="Top" Width="220" /> </Grid>  
  9. </Page>  

Step 4

Deploy your app on Local Machine. The output of the UWPTxtCopyPasteApp is shown below.

 

After entering the text, click the Copy button.

 

Clicking the Paste button -

 

Summary

Now, you have successfully tested how to copy and paste using the clipboard in Visual C# and UWP environment 2017.