Toast 标题Toast headers

通过在通知中使用 toast 标题,可在操作中心中直观地将相关通知进行归组。You can visually group a set of related notifications inside Action Center by using a toast header on your notifications.

重要

需要桌面创意者更新和 1.4.0 的通知库 :必须运行桌面内部版本 15063 或更高版本才可查看 toast 标题。Requires Desktop Creators Update and 1.4.0 of Notifications library : You must be running Desktop build 15063 or higher to see toast headers. 必须使用版本 1.4.0 或更高版本的 UWP 社区工具包通知 NuGet 库来构造 toast 内容中的标题。You must use version 1.4.0 or higher of the UWP Community Toolkit Notifications NuGet library to construct the header in your toast's content. 仅桌面设备支持标题。Headers are only supported on Desktop.

如下所示,这组对话统一在标题“野营!!”下。As seen below, this group conversation is unified under a single header, "Camping!!". 对话中的每条单独消息都是一个独立的 toast 通知,它们具有相同的 toast 标题。Each individual message in the conversation is a separate toast notification sharing the same toast header.

Toasts with header

也可以选择按类别对通知进行直观分组,例如航班提醒、包裹跟踪等。You can also choose to visually group your notifications by category too, like flight reminders, package tracking, and more.

向 toast 添加标题Add a header to a toast

下面介绍如何向 toast 通知添加标题。Here's how you add a header to a toast notification.

备注

仅桌面设备支持标题。Headers are only supported on Desktop. 不支持标题的设备会直接忽略标题。Devices that don't support headers will simply ignore the header.

new ToastContentBuilder()
    .AddHeader("6289", "Camping!!", "action=openConversation&id=6289")
    .AddText("Anyone have a sleeping bag I can borrow?");

总结...In summary...

  1. ToastContent 添加 HeaderAdd the Header to your ToastContent
  2. 指定所需的 IdTitleArguments 属性Assign the required Id , Title , and Arguments properties
  3. 发送通知(了解详细信息Send your notification (learn more)
  4. 在另一个通知上,使用相同的标题 Id ,以将其统一到该标题下。On another notification, use the same header Id to unify them under the header. Id 是用于确定是否应对通知进行分组的唯一属性,也就是说 TitleArguments 可以不同。The Id is the only property used to determine whether the notifications should be grouped, meaning the Title and Arguments can be different. 会使用组内最新通知中的 TitleArgumentsThe Title and Arguments from the most recent notification within a group are used. 如果该通知已删除,则使用其次最新通知中的 TitleArgumentsIf that notification gets removed, then the Title and Arguments falls back to the next most recent notification.

从标题中处理激活Handle activation from a header

用户可以点击标题,以便用户可通过此操作获取应用中的更多信息。Headers are clickable by users, so that the user can click the header to find out more from your app.

因此,应用可在标题上提供 参数 ,类似于 toast 本身的启动参数。Therefore, apps can provide Arguments on the header, similar to the launch arguments on the toast itself.

激活的处理方式与 常规 toast 激活相同,这意味着可以在 App.xaml.csOnActivated 方法中检索这些参数,就像用户单击 toast 主体或 toast 上的按钮时一样。Activation is handled identical to normal toast activation, meaning you can retrieve these arguments in the OnActivated method of App.xaml.cs just like you do when the user clicks the body of your toast or a button on your toast.

protected override void OnActivated(IActivatedEventArgs e)
{
    // Handle toast activation
    if (e is ToastNotificationActivatedEventArgs)
    {
        // Arguments specified from the header
        string arguments = (e as ToastNotificationActivatedEventArgs).Argument;
    }
}

其他信息Additional info

标题可直观地分离通知并将其归组。The header visually separates and groups notifications. 它不会更改有关应用可以具有的最大通知数 (20) 以及通知列表的先入先出行为的任何其他逻辑。It doesn't change any other logistics about the maximum number of notifications an app can have (20) and the first-in-first-out behavior of the notifications list.

标题内的通知顺序如下... 对于给定的应用,会首先显示应用(如果是标题的一部分,则为整个标题组)中最新的通知。The order of notifications within headers are as follows... For a given app, the most recent notification from the app (and the entire header group if part of a header) will appear first.

Id 可以是所选择的任何字符串。The Id can be any string you choose. ToastHeader 中的任何属性都没有长度或字符限制。There are no length or character restrictions on any of the properties in ToastHeader . 唯一的限制是整个 XML toast 内容不能大于 5KB。The only constraint is that your entire XML toast content cannot be larger than 5 KB.

在出现“查看详细信息”按钮前,创建标题不会更改操作中心内显示的通知数量(此数量默认为 3,并且用户可以在系统的通知“设置”中为每个应用配置此数量)。Creating headers doesn't change the number of notifications shown inside Action Center before the "See more" button appears (this number is 3 by default and can be configured by the user for each app in system Settings for notifications).

单击标题(就像单击应用程序标题一样)不会清除属于此标题的任何通知(应用应使用 toast API 来清除相关通知)。Clicking on the header, just like clicking on the app title, does not clear any notifications belonging to this header (your app should use the toast APIs to clear the relevant notifications).