偶然需要在应用中访问剪贴板,其实和SL访问剪贴板是一样的。
参考下MSDN:
写得清楚明白:
Clipboard Class
:
System.Windows.Clipboard
Namespace:
Assembly: System.Windows (in System.Windows.dll)
有三个方法:
Queries the clipboard for the presence of data in the UnicodeText format. Retrieves Unicode text data from the system clipboard, if Unicode text data exists. Sets Unicode text data to store on the clipboard, for later access with .
看名字就能知道用法了额
ContainsText返回值是bool型,用于查看Clipboard是否有内容
GetText 是从Clipboard取值
SetText 是往Clipboard赋值
不过要注意GetText 和SetText 可能会引起SecurityException哦
参见MSDN:和
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: 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 or event. For examples of situations that are not considered user-initiated, clipboard access is not valid from a handler for 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 .
简单说就是GetText和SetText 在Click 或 KeyDown事件中是被允许的,在 Loaded事件或构造函数中是被禁止访问的。
在使用的时候要注意额…
(PS:试了下在WP里SetText 是可以在Loaded使用,而GetText会报异常)