在柯塔娜后台任务将剪贴板中的内容 [英] Set Clipboard content in Cortana background task

查看:486
本文介绍了在柯塔娜后台任务将剪贴板中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想内容后台任务到剪贴板,但我不能得到它的工作。这里是我的代码:

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:

激活从MTA单线程类不支持
(从HRESULT异常:0x8000001D)的System.Exception
{} System.Runtime.InteropServices.COMException

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



抛出System.NullReferenceException

Throws a System.NullReferenceException

推荐答案

第一条错误消息是很清楚的。剪贴板预期的 STA 线程。而对于用C#(你的情况)或C ++开发的应用程序,后台任务在一个进程内DLL(由应用程序或专用BackgroundtaskHost.exe加载)是在 MTA 举行。

The first error message is very clear. The clipboard expect STA thread. And for the app developed by c# (your case) or c++, the background tasks are hosted in an in-proc DLL (loaded by the app or the dedicated BackgroundtaskHost.exe) that is in MTA.

有两种情况:


  1. 前沿的应用程序是运行模式:
    中的coredispatcher可以用来问UI STA线程来执行操作。

  1. Forefront app is in running mode: The coredispatcher can be used to ask the UI STA thread to perform the action.

前沿的应用程序被暂停或终止:
后台任务(当应用程序写在C#和C ++)始终在MTA模式和UI STA主题不存在,所以我们不能在后台任务中使用剪贴板这种情况下,如果该类不从MTA支持激活。

Forefront app is suspended or terminated: The background task (when app written in c# and c++) always runs in MTA mode and the UI STA thread doesn't exist, so we cannot use Clipboard in background task for this scenario if the class doesn't support the activation from MTA.

所以记住这一点:

有关后台任务共享状态的唯一可靠方法是使用持久性存储,如的ApplicationData或文件。

The only reliable way for the background task to share state is to use persistent storage, such as ApplicationData, or files.

这篇关于在柯塔娜后台任务将剪贴板中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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