Silverlight 4 clipboard access with the Elevated permission on


With the release of Silverlight 4, now the Silverlight has support for cross platform Clipboard access. Prior to this just IE only solution exist. 

The Clipboard API (System.Windows.Clipboard) comes with below three static method.
  • GetText, retrieves value from the clipboard 
  • SetText, places value on the clipboard 
  • ContainsText, to check whether the clipboard currently contains value 
By default when you use Clipboard feature first time in Silverlight you will be prompted to allow your application to access your system clipboard. To demonstrate this feature I will create a sample application, below are the step by step approach for the same.

1. Create Silverlight Application project

1.gif
 
2. Add UI content

For this example I am adding two text boxes which will be used as source (copy content from) and destination (paste content to) and two buttons, one for copy and another for paste. Add below code in MainPage.xaml.

<Grid x:Name="LayoutRoot" Background="#FFB2C1C1">
        <Grid Margin="20,20,20,20">
            <Grid.RowDefinitions>
                <RowDefinition Height="24"/>
                <RowDefinition Height="24"/>
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal">
                <TextBox x:Name="txtCopy" Width="100" Height="20"/>
                <Button x:Name="btnCopy" Content="Copy" Height="20" Click="btnCopy_Click"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <TextBox x:Name="txtPaste" Width="100" Height="20"/>
                <Button x:Name="btnPaste" Content="Paste" Height="20" Click="btnPaste_Click"/>
            </StackPanel>
        </Grid>
</Grid>

Added below code in MainPage.xaml.cs

private void btnCopy_Click(object sender, RoutedEventArgs e)
{
    Clipboard.SetText(txtCopy.Text);
}
private void btnPaste_Click(object sender, RoutedEventArgs e)
{
    txtPaste.Text = Clipboard.GetText();
}

3. Run the application 

Run this application, you will get below UI.

2.gif
 
Type some text in first text box and click on Copy button. As soon as you click on copy button you will be prompted to allow this application to access clipboard. 

3.gif
 
This confirmation dialog will be displayed first time you click on copy button, until your application is running you will not get the prompted again. But when you close your application and run it again you will again get this prompt, to avoid this you may check "Remember my answer" checkbox so that application remembers your setting and you will not get prompted on next run. 

When you click Yes button on above confirmation dialog and click on Paste button the text will be pasted into second text box.

If you click on No button, you will get security exception.

4.gif
 
On the next step we will see how to use Elevated permission to avoid Clipboard prompt.

4. Enable Elevated permission to avoid Clipboard access prompt

In Out of Browser application you can enable Elevated permission, when it is enabled you will not get prompt for Clipboard access. You can enable this by checking "Require elevated trust when running outside the browser" checkbox from Out of Browser Settings dialog box.

5.gif

In this article we have seen step by step approach to implement Clipboard functionality in Silverlight application. And we have seen when Elevated permission for Out of Browser application is enabled the Silverlight does not prompt for Clipboard access.

Up Next
    Ebook Download
    View all
    Learn
    View all