使用后台任务设置剪贴板内容[Windows 10] [UWP] [英] Setting Clipboard content using a Background Task [Windows 10] [UWP]

查看:41
本文介绍了使用后台任务设置剪贴板内容[Windows 10] [UWP]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Universal Windows 10应用程序.目前,我有一个后台任务,一旦用户收到通知,就会触发该任务.此BG任务的目的是复制通知的内容.问题在于Clipboard.setcontent方法似乎是单线程的,而不是多线程的BG任务.我曾经尝试过使用corewindow调度程序,但是那没有用,我也尝试过使用任务.有人可以指出我的解决方案吗?

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:

不支持从MTA激活单线程类(HRESULT的异常:0x8000001D).

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

代码:

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

这篇关于使用后台任务设置剪贴板内容[Windows 10] [UWP]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆