Note – these posts are put together after a short time with Silverlight 4 as a way of providing pointers to some of the new features that Silverlight 4 has to offer. I’m posting these from the PDC as Silverlight 4 is announced for the first time so please bear that in mind when working through these posts.
I must use the clipboard hundreds of times a day. Business applications need clipboard access. Silverlight 3 only provided clipboard access in the form of a couple of built-in controls ( like TextBox ) providing copy/paste automatically like most TextBoxes in the world already do.
But in Silverlight 4 this is expanded to allow for more complete clipboard access.
Like many features in Silverlight, clipboard access must be in response to a user-initiated access ( i.e. you can’t write code on some background thread that tries to grab the clipboard – you must run this kind of code in response to a user action like a button click ).
Also, the formats for data on the clipboard are restricted. At the time of writing, the only format supported is text and so the clipboard class has 3 simple methods;
- ContainsText
- GetText
- SetText
So, it’s beyond simple right now to interact with the Clipboard. If we have a UI such as;
<UserControl x:Class="SilverlightApplication9.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button
Margin="10"
Content="Paste"
Click="Paste" />
<TextBlock
x:Name="txtText"
Margin="10"
Text="{Binding Text}"
Grid.Row="1" />
<Button
Margin="10"
Click="Copy"
Content="Copy"
Grid.Row="2" />
</Grid>
</UserControl>
and a little bit of code running behind it such as;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication9
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Paste(object sender, RoutedEventArgs e)
{
if (Clipboard.ContainsText())
{
txtText.Text = Clipboard.GetText();
}
}
private void Copy(object sender, RoutedEventArgs e)
{
Clipboard.SetText(txtText.Text);
}
}
}
then running that up gives me a UI like;
and clicking that Paste button puts up a consent UI;
If the user clicks No then the call to Clipboard.GetText() throws so that’s worth knowing about whereas if they click Yes then all’s well;
So – easy enough to have Clipboard access ( for text ) in a Silverlight 4 application. I’m hoping that one or two more formats might be enabled ( e.g. Images? ) as previews progress.
Posted
Wed, Nov 18 2009 11:10 AM
by
mtaulty