I develop an app who paste content to the clipboard from it's background task.
public sealed class ToastBackgroundTask : IBackgroundTask {
public void Run(IBackgroundTaskInstance taskInstance) {
//Inside here developer can retrieve and consume the pre-defined
//arguments and user inputs;
var toastArgs = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;
var argument = toastArgs.Argument;
SetClipbordContent(toastArgs.Argument);
}
public static void SetClipbordContent(string text) {
var dataPackage = new DataPackage();
dataPackage.SetText(text);
Clipboard.SetContent(dataPackage);
}
}
But when I execute the line
Clipboard.SetContent(dataPackage);
raise this exeption:
The activation of a single-threaded class from MTA is not supported.
(Exception from HRESULT: 0x8000001D)
An usual workaround to handle this secario in classic .Net Framework is to use the Thread class to execute this portion of code in a STA context (C# Clipboard.GetText()) But I don't know how to do this in UWP.
I have used this in the past to execute on the UI thread from a background thread...
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => {
// Do something...
}
User contributions licensed under CC BY-SA 3.0