Setting Clipboard content using a Background Task [Windows 10] [UWP]

5

I'm working on a Universal Windows 10 App. At the moment I have a background task that gets triggered once the user receives a notification. The purpose of this BG task is to copy the content of the notification. The problem is that the Clipboard.setcontent method appears to be single threaded as opposed to the multi threaded BG task. I have tried using the corewindow dispatcher but that didn't work, I also tried using tasks. Could someone point me out to the solution please?

E.g. the following code in a background task throws the exception:

Activating a single-threaded class from MTA is not supported (Exception from HRESULT: 0x8000001D).

Code:

var dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy };
dataPackage.SetText("Hello World!");
Clipboard.SetContent(dataPackage);
c#
uwp
asked on Stack Overflow Aug 11, 2015 by Saeed Sefrini • edited Jul 5, 2018 by Uwe Keim

1 Answer

4

Save the content somewhere and assign the string to the Clipboard while the app is about to resume.

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
    var dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy };
    dataPackage.SetText("Hello World!");
    Clipboard.SetContent(dataPackage);

    getText();
});

private async void getText()
{
    string t = await Clipboard.GetContent().GetTextAsync();
}
answered on Stack Overflow Aug 24, 2015 by James He • edited Jul 5, 2018 by Uwe Keim

User contributions licensed under CC BY-SA 3.0