【问题标题】:UWP: Ask for Clipboard acces from Background TaskUWP:从后台任务请求剪贴板访问权限
【发布时间】:2016-05-29 11:26:00
【问题描述】:

我开发了一个应用程序,它从后台任务将内容粘贴到剪贴板。

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

但是当我执行该行时

Clipboard.SetContent(dataPackage);

提出这个例外:

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

在经典 .Net Framework 中处理此 secario 的常用解决方法是使用 Thread 类在 STA 上下文中执行这部分代码 (C# Clipboard.GetText()) 但我不知道如何在 UWP 中执行此操作。

【问题讨论】:

  • Afaik 不幸的是,这是不可能的。 Bg 任务不应该做前台的事情,因此这样的事情是不允许的。我还希望至少可以为几个很好的用例设置剪贴板,例如 2FA 或密码管理器集成到 Cortana。
  • 这里有一个类似的帖子(Set Clipboard content in Cortana background task)供您参考。

标签: c# .net windows uwp windows-applications


【解决方案1】:

我过去曾使用它从后台线程在 UI 线程上执行...

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => {
    // Do something...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多