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/