c# - Windows 10通用应用程序。如何触发后台任务?

标签 c# notifications win-universal-app background-task

我需要通过按下操作按钮从交互式 toast 通知触发后台任务。我不知道我做错了什么。我可以注册一个任务并在 visual studio 中看到它。即使我可以调试它(调试器跳转到 MyToastNotificationBackgroundTask.Run 函数,但参数 IBackgroundTaskInstance taskInstance 是空对象),单击按钮永远不会运行该任务,或者至少调试器不会显示它。

我正在注册这样的后台任务

var builder = new BackgroundTaskBuilder();
builder.Name = "MyToastNotificationBackgroundTask";
builder.TaskEntryPoint = "Tasks.MyToastNotificationBackgroundTask";
builder.SetTrigger(new ToastNotificationActionTrigger());
BackgroundTaskRegistration task = builder.Register();

显示通知

ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
ScheduledToastNotification myToastNotificaton = new ScheduledToastNotification(this.myToastXml, DateTime.Now.AddMinutes(1), TimeSpan.FromMinutes(60), 2);
myToastNotificaton .Id = "toast_54ahk36s";
toastNotifier.AddToSchedule(myToastNotificaton);

在应用 list 中

<Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="Tasks.MyToastNotificationBackgroundTask">
      <BackgroundTasks>
        <Task Type="systemEvent" />
      </BackgroundTasks>
    </Extension>
</Extensions>

在 toast 模板中,xml 操作按钮是

<actions>
    <input id="message" type="text" placeholderContent="200" />
    <action activationType="background" content="Count" arguments="count" />
</actions>

后台任务itsetf

namespace Tasks
{
    public sealed class MyToastNotificationBackgroundTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;
            ...
        }
    }
}

我不明白在通知模板操作按钮上指定 activationype="background"与 MyToastNotificationBackgroundTask 任务有什么关系?我找不到相关信息。

有人请分享你的知识。也许你有一个工作示例或 smf。任何帮助将不胜感激。提前致谢。

最佳答案

I can't understand how specifying activationype="background" on a notification templates action button is relating to MyToastNotificationBackgroundTask task? i can't find relevant info on that.

后台任务的触发类型(ToastNotificationActionTrigger)是连接Toast Action 和后台任务的。当用户点击操作时,应用会查找带有 ToastNotificationActionTrigger 触发器的后台任务,如果找到则运行。

我使用了您的代码但无法重现问题,触发器工作正常。我的猜测是您已经使用该名称注册了一个任务,但没有正确的触发器类型(先尝试取消注册)-或者-您在 toast xml(activationType 字段)中存在拼写问题。

关于c# - Windows 10通用应用程序。如何触发后台任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33835440/

相关文章:

javascript - Chrome 桌面通知示例

c# - 事件处理程序未在动态按钮单击时触发

c# - 将 NULL 插入 DataTable

ios - 如何在早上 8 点发出每日通知? ( swift 3)

c# - 如何在通用 Windows 应用程序中设计响应式 UI

c# - 如何将外部 Xaml 页面链接到通用 Windows 平台中的网格?

c# - 如何访问 Windows 10 UWP 中的联系人?

c# - MVC 项目中的 .Rdlc 报告 - 托管调试助手 'PInvokeStackImbalance'

c# - "The remote certificate is invalid according to the validation procedure"wcf

android - 在远程 View 通知中更改 Imageview 资源