Windows Phone by default has a builtin mechanism to support copy and paste functionality, but in some case we may need to handle the copy experience explicitly.
We won't be able to paste outside a TextBox, since this is handled by the Operating System functionality. So, there is no need to handle this in code.
The Windows Phone “System.Windows” namespace exposes the Clipboard class having the following three methods to handle the Clipboard. They are:
- ContainsText()
- GetText()
- SetText(string)
ContainsText(): The ContainsText() method queries the clipboard for the presence of data in the Unicode text format and returns a bool value of true or false.
Code snippet
GetText(): The GetText() method helps to get the copied text from the clipboard. This method returns the copied Unicode text data if text is present in the clipboard, otherwise it returns an empty string.
Code snippet
Clipboard.GetText();
SetText(string text): The SetText(string text) method sets Unicode text to store/copy on the clipboard.
Code snippet
Clipboard.SetText("text to copy");
Now, here is the demo for Windows Phone 8.
Create a new Windows Phone 8 project and create one button and two textboxes as shown in the following clipboard operation.
Figure 1: PageName
XAML Code
- <phone:PhoneApplicationPage
- x:Class="ClipBoardDemo.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
- xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- SupportedOrientations="Portrait" Orientation="Portrait"
- shell:SystemTray.IsVisible="True">
-
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
-
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
- <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
-
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
- <Button Content="CopyToClipBoard" HorizontalAlignment="Left" Margin="107,229,0,0" Grid.Row="1" VerticalAlignment="Top" Width="260" Click="Button_Click_1"/>
- <TextBox HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="456" Margin="0,321,0,0" Grid.Row="1"/>
- <TextBox x:Name="clipboardTextbox" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="456" Margin="2,152,0,0" Grid.Row="1"/>
- </Grid>
- </phone:PhoneApplicationPage>
Now, write the following code for the button click event.
C# Code
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- Clipboard.SetText(clipboardTextbox.Text.ToString());
- }
Next, run your application and enter some text into the clipboardTextbox as shown in the following screenshot.
Figure 2: Enter text in the ClipBoard TextBox
Click the CopyToClipBoard button to copy the text to the clipboard. Now, place the cursor onto another TextBox and you can see the paste symbol, press paste to paste the copied text as shown in the following screenshot.
Figure 3: Copy To ClipBoard