简体   繁体   English

使用后台任务设置剪贴板内容[Windows 10] [UWP]

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

I'm working on a Universal Windows 10 App. 我正在开发通用Windows 10应用程序。 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. 此BG任务的目的是复制通知的内容。 The problem is that the Clipboard.setcontent method appears to be single threaded as opposed to the multi threaded BG task. 问题是Clipboard.setcontent方法似乎是单线程的,而不是多线程BG任务。 I have tried using the corewindow dispatcher but that didn't work, I also tried using tasks. 我尝试过使用corewindow调度程序,但是没有用,我也尝试过使用任务。 Could someone point me out to the solution please? 有人能指出我的解决方案吗?

Eg the following code in a background task throws the exception: 例如,后台任务中的以下代码会抛出异常:

Activating a single-threaded class from MTA is not supported (Exception from HRESULT: 0x8000001D). 不支持从MTA激活单线程类(HRESULT的异常:0x8000001D)。

Code: 码:

var dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy };
dataPackage.SetText("Hello World!");
Clipboard.SetContent(dataPackage);

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM