偶然需要在应用中访问剪贴板,其实和SL访问剪贴板是一样的。
参考下MSDN:http://msdn.microsoft.com/en-us/library/system.windows.clipboard%28VS.95%29.aspx
写得清楚明白:
Clipboard Class
System.Object
System.Windows.Clipboard
Namespace: System.Windows
Assembly: System.Windows (in System.Windows.dll)
有三个方法:
ContainsText
Queries the clipboard for the presence of data in the UnicodeText format.
GetText
Retrieves Unicode text data from the system clipboard, if Unicode text data exists.
SetText
Sets Unicode text data to store on the clipboard, for later access with GetText.
看名字就能知道用法了额
ContainsText返回值是bool型,用于查看Clipboard是否有内容
GetText 是从Clipboard取值
SetText 是往Clipboard赋值
不过要注意GetText 和SetText 可能会引起SecurityException哦
参见MSDN:http://msdn.microsoft.com/en-us/library/ms597033(v=vs.95).aspx和http://msdn.microsoft.com/en-us/library/ms597043(v=vs.95).aspx
GetText SecurityException:
Invoked this method from outside a user-initiated context.
Clipboard access user dialog box was not confirmed.
Invoked this method in a Windows Phone application.
SetText SecurityException:
Invoked this method from outside a user-initiated context.
Clipboard access user dialog box was not confirmed.
Under partial trust (the default mode), Silverlight restricts clipboard access to its two key APIs: GetText and SetText. These APIs can only be invoked from within a context that is determined by the Silverlight runtime to be in response to a user-initiated action. For example, clipboard access is valid from within a handler for a Click or KeyDown event. For examples of situations that are not considered user-initiated, clipboard access is not valid from a handler for Loaded or from a constructor. Clipboard access under partial trust also requires the user to confirm access through completion of a Silverlight access-confirmation dialog box.
If you specifically produce an out-of-browser application and request elevated trust, this security restriction on the API and its dialog box are not used. For more information, see Trusted Applications.
简单说就是GetText和SetText 在Click 或 KeyDown事件中是被允许的,在 Loaded事件或构造函数中是被禁止访问的。
在使用的时候要注意额…
(PS:试了下在WP里SetText 是可以在Loaded使用,而GetText会报异常)
本文转自 sun8134 博客园博客,原文链接:http://www.cnblogs.com/sun8134/archive/2012/03/18/2405217.html ,如需转载请自行联系原作者