Silverlight 4 Beta communication with the Clipboard

Source: Internet
Author: User
Tags copy gettext silverlight

In my previous article "Silverlight4beta's right mouse button support" mentioned that SL4 finally support the right mouse button. Although a context menu control is not provided, it is not difficult to achieve it. The most common operation of context menus is to copy/paste/cut This type of functionality, and today we'll talk about another simple new feature of Silverlight4beta: Communicating with the Clipboard (in previous SL versions, because there is no built-in support for the Clipboard communication, we can only call JS's means to realize the communication between SL and the shearing board)

Yes, SL4 really supports the clipboard, but it's a pathetic little feature.

All the features of the Clipboard are located below the System.Windows.Clipboard class, so let's look at its member definitions:

public static bool ContainsText(); 剪切板中是否包含文本 
public static string GetText(); 得到剪切板中的文本内容
  public static void SetText(string text); 设置剪切板的文本内容

Only these three methods. The members are really few pitiful, the function is indeed simply excessive. As I added above, SL4 only supports text information in the Clipboard, what pictures, other multimedia information, don't even think about it.

Next we do a simple example, the code is very simple, it is not explained in detail.

XAML is as follows

<Grid x:Name="LayoutRoot" Background="White">
   <StackPanel Orientation="Horizontal" Height="40">
     <TextBox AcceptsReturn="True" x:Name="txtText" Width="150" />
     <Button Content="复制" Click="Copy" />
     <Button Content="粘贴" Click="Paste" />
   </StackPanel>
</Grid>

C # code is as follows

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) {
     if (!string.IsNullOrEmpty(txtText.Text.Trim()))
       Clipboard.SetText(txtText.Text);
   }
}

The operation effect is as follows:

Another safety issue, when SL first communicates with the Clipboard, requires user approval

 

A detailed explanation of security can be found in my blog post: "Silverlight4beta's operating Camera/microphone" in the relevant content.

Ok,have fun~

Source: http://024hi.cnblogs.com/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.