Silverlight 4 Clipboard access

Silverlight 4 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:

Silverlight clipboard sample

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)

Technorati Tags:
Published Saturday, November 21, 2009 3:20 PM by pluginbaby
Filed under:

Comments

Saturday, November 21, 2009 3:37 PM by DotNetShoutout

# Silverlight 4 Clipboard access - Laurent Duveau

Thank you for submitting this cool story - Trackback from DotNetShoutout

Saturday, November 21, 2009 3:57 PM by Silverlight 4 Clipboard access - Laurent Duveau

# Silverlight 4 Clipboard access - Laurent Duveau

Pingback from  Silverlight 4 Clipboard access - Laurent Duveau

Saturday, November 21, 2009 6:50 PM by Silverlight 4 Clipboard access - Laurent Duveau iSilverlight

# Silverlight 4 Clipboard access - Laurent Duveau iSilverlight

Pingback from  Silverlight 4 Clipboard access - Laurent Duveau iSilverlight

# Twitter Trackbacks for Silverlight 4 Clipboard access - Laurent Duveau [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Silverlight 4 Clipboard access - Laurent Duveau         [asp.net]        on Topsy.com

Sunday, November 22, 2009 12:24 AM by uberVU - social comments

# Social comments and analytics for this post

This post was mentioned on Twitter by ShawnWildermuth: RT @LaurentDuveau Blogged: Silverlight 4 Clipboard access - http://tinyurl.com/y9o4lgy

Sunday, November 22, 2009 1:35 AM by progg.ru

# Доступ к буферу обмена в Silverlight 4

Thank you for submitting this cool story - Trackback from progg.ru

# Links (11/22/2009) « Steve Pietrek – Everything SharePoint

Pingback from  Links (11/22/2009) « Steve Pietrek – Everything SharePoint

Sunday, December 06, 2009 12:17 PM by Micha?? Jask??lski » Silverlight 4

# Micha?? Jask??lski » Silverlight 4

Pingback from  Micha?? Jask??lski » Silverlight 4

Tuesday, December 08, 2009 6:35 PM by Community Blogs

# Silverlight 4 presentation at the Montreal UG

Unfortunately we had Jesse Liberty down by H1N1, so I took care of the Silverlight 4 session with co

Monday, January 18, 2010 2:41 AM by Rajneesh Noonia

# re: Silverlight 4 Clipboard access

Silverlight clipboard access was also posible with Silverlight 3 with simple tech.I have just published the clipboard class for SL3 which is more power full than current clipboard class in SL4.This can also be used in SL4. Check at my blog ..

rajneeshnoonia.blogspot.com/.../silvetrlight-3-implementing-clipboard.html