Reputation: 39
In my UWP Application When I am trying to set the content in the Clipboard
I am getting the below exception.
Targeted SDK version: Windows 10 SDK, version 1903
System.Exception: The remote procedure call failed. (Exception from HRESULT: 0x800706BE) at Windows.ApplicationModel.DataTransfer.Clipboard.SetContent(DataPackage content)
public static void CopyTextToClipboard(string textToCopy)
{
if (!string.IsNullOrEmpty(textToCopy))
{
var dataPackage = new DataPackage();
dataPackage.RequestedOperation = DataPackageOperation.Copy;
dataPackage.SetText(textToCopy.Trim().Replace(" ", string.Empty));
Clipboard.SetContent(dataPackage);
}
}
Upvotes: 1
Views: 292
Reputation: 39072
Clipboard.SetContent
must be called from the UI thread. In case you are executing this code from a different thread, you must wrap the Clipboard
call inside a Dispatcher.RunAsync
:
await CoreApplication.MainView.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() => Clipboard.SetContent(dataPackage));
Also, note this means Clipboard
cannot be used from a background service as well (this has no associated UI thread).
Upvotes: 1