Reputation: 877
I need to set some text returned from a WCF service asynchronously in clipboard. The problem with Clipboard class in that it it thread safe, thus on setting the text returned from service, it generates Security Exception - "Clipboard access not allowed" and I am unable to copy my text to clipboard.
Can anyone please suggest a solution..
Upvotes: 1
Views: 77
Reputation: 247343
According to clipboard documentation here
In partial trust (the default mode for browser-hosted Silverlight-based applications), Silverlight also 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. In contrast, clipboard access is not valid from a handler for Loaded or from a constructor, and access attempts throw exceptions.
In addition, Silverlight prompts the user for confirmation if the clipboard is accessed under partial trust. This Silverlight access-confirmation dialog box is displayed one time per session. 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.
That said this is the suggested solution for this limitation.
When the data is returned from the service, it should be stored in a variable and the user presented with a button to click where the data will then be copied to the clipboard.
string data = "";
...
void LoadDataAsync() {
//Service call populates variable.
//When service call completes Button is enabled allowing user to click
}
...
private void OnButtonClick(object sender, System.Windows.RoutedEventArgs e) {
System.Windows.Clipboard.SetText(data);
}
You could present the user with a dialog when the service completes notifying user that download has completed and ask id they want to copy data to clipboard. That will give you the user initiated action needed to allow the Clipboard to work.
Upvotes: 1