具有挂起更新激活的 ToastToast with pending update activation

可以使用 PendingUpdate 在 Toast 通知内创建多步骤交互。You can use PendingUpdate to create multi-step interactions in your toast notifications. 例如,如下所示,可以创建一系列的 Toast,其中后续的 Toast 依赖来自先前 Toast 的响应。For example, as seen below, you can create a series of toasts where the subsequent toasts depend on responses from the previous toasts.

有待更新的 Toast

重要

需要桌面秋季创意者更新和 2.0.0 的通知库:必须运行桌面版本 16299 或更高版本才能查看挂起的更新工作。Requires Desktop Fall Creators Update and 2.0.0 of Notifications library: You must be running Desktop build 16299 or higher to see pending update work. 必须使用版本 2.0.0 或更高版本的 UWP 社区工具包通知 NuGet 库来分配按钮上的 PendingUpdateYou must use version 2.0.0 or higher of the UWP Community Toolkit Notifications NuGet library to assign PendingUpdate on your buttons. PendingUpdate 仅支持桌面设备,在其他设备上会被忽略。PendingUpdate is only supported on Desktop and will be ignored on other devices.

先决条件Prerequisites

本文假定读者具备后列应用知识:This article assumes a working knowledge of...

概述Overview

实现一个 Toast,该 Toast 使用挂起的更新作为其激活后行为…To implement a toast that uses pending update as its after activation behavior...

  1. 在 Toast 后台激活按钮上指定 PendingUpdateAfterActivationBehaviorOn your toast background activation buttons, specify an AfterActivationBehavior of PendingUpdate

  2. 在发送 Toast 时分配 Tag(或 GroupAssign a Tag (and optionally Group) when sending your toast

  3. 当用户单击按钮时,会激活后台任务,Toast 会保持在屏幕上并处于挂起更新状态When the user clicks your button, your background task will be activated, and the toast will be kept on-screen in a pending update state

  4. 在后台任务中,使用相同的 TagGroup,发送含有新内容的新 ToastIn your background task, send a new toast with your new content, using the same Tag and Group

分配 PendingUpdateAssign PendingUpdate

在后台激活按钮上,将 AfterActivationBehavior设置为 PendingUpdateOn your background activation buttons, set the AfterActivationBehavior to PendingUpdate. 请注意,这仅适用于具有 BackgroundActivationType 的按钮。Note that this only works for buttons that have an ActivationType of Background.

new ToastContentBuilder()

    .AddText("Would you like to order lunch today?")

    .AddButton(new ToastButton("Yes", "action=orderLunch")
    {
        ActivationType = ToastActivationType.Background,

        ActivationOptions = new ToastActivationOptions()
        {
            AfterActivationBehavior = ToastAfterActivationBehavior.PendingUpdate
        }
    });

在通知上使用“标记”Use a Tag on the notification

若要以后替换该通知,必须在通知上分配 Tag(或 Group)。In order to later replace the notification, we have to assign the Tag (and optionally the Group) on the notification.

// Create the notification
var notif = new ToastNotification(content.GetXml())
{
    Tag = "lunch"
};

// And show it
ToastNotificationManager.CreateToastNotifier().Show(notif);

使用新内容替换 ToastReplace the toast with new content

作为对用户单击按钮的响应,会触发后台任务,并需要使用新内容替换 Toast。In response to the user clicking your button, your background task gets triggered and you need to replace the toast with new content. 只需通过使用相同的 TagGroup 发送新 Toast 来替换 Toast。You replace the toast by simply sending a new toast with the same Tag and Group.

强烈建议在替换时将音频设置为静音模式以响应按钮单击,因为用户已经在与 Toast 交互。We strongly recommend setting the audio to silent on replacements in response to a button click, since the user is already interacting with your toast.

// Generate new content
ToastContent content = new ToastContent()
{
    ...

    // We disable audio on all subsequent toasts since they appear right after the user
    // clicked something, so the user's attention is already captured
    Audio = new ToastAudio() { Silent = true }
};

// Create the new notification
var notif = new ToastNotification(content.GetXml())
{
    Tag = "lunch"
};

// And replace the old one with this one
ToastNotificationManager.CreateToastNotifier().Show(notif);