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;
So, it’s beyond simple right now to interact with the Clipboard. If we have a UI such as;
<Grid x:Name="LayoutRoot" Background="White">
and a little bit of code running behind it such as;
public partial class MainPage : UserControl
private void Paste(object sender, RoutedEventArgs e)
txtText.Text = Clipboard.GetText();
private void Copy(object sender, RoutedEventArgs e)
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.
Wed, Nov 18 2009 11:10 AM