Silverlight 4 Clipboard access
Silverlight 4 (currently in beta) adds support for Clipboard!
In Silverlight 3 we had copy/paste available in the Textbox, programmatic access could be done, some IE only solution exists via HTML DOM bridge (so not OOB) or cross-browser (involving Flash!). But now the game has changed as we now have an API for multi-platform Clipboard access.
In this beta, the support is for (Unicode) text-only, and the Clipboard class has 3 static methods:
- GetText()
- SetText()
- ContainsText()
Clipboard access can only be done from a user initiated action (mouse, keyboard), and user is prompted to acknowledge the first time Clipboard is set or read (once per session).
If the application is running Out Of Browser and under the new elevated permission mode, user gets no prompt and Clipboard methods can be used anytime (not only during user initiated actions).
I built a small sample to try this:
The code is pretty simple:
private void btnCopy_Click(object sender, RoutedEventArgs e) { if (txtCopy.SelectedText.Length > 0) Clipboard.SetText(txtCopy.SelectedText); else MessageBox.Show("Nothing to copy!"); } private void btnPaste_Click(object sender, RoutedEventArgs e) { if (Clipboard.ContainsText()) txtPaste.Text = Clipboard.GetText(); }
Note you can also use system clipboard (so text from outside Silverlight works as well), and you can use ctrl+c / ctrl+v instead of the buttons.
Download source code
(requires Silverlight 4 beta)