I am trying to add content to the Clipboard in a background task but I can't get it to work. here is my Code:
var dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy };
dataPackage.SetText("EUREKA!");
Clipboard.Flush();
Clipboard.SetContent(dataPackage);
I get the error Message:
Activating a single-threaded class from MTA is not supported (Exception from HRESULT: 0x8000001D) System.Exception {System.Runtime.InteropServices.COMException}
I found a similar question with a Notification and not Cortana but the proposed solution:
private async Task CopyToClipboard(string strText)
{
CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
var dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy };
dataPackage.SetText("EUREKA!");
Clipboard.SetContent(dataPackage);
getText();
});
}
private async void getText()
{
string t = await Clipboard.GetContent().GetTextAsync();
}
Throws a System.NullReferenceException