在 Windows 小组件板中显示 PWA 小组件

各种操作系统都有小部件仪表板,可让用户阅读内容和执行任务。 示例包括 Android 主屏幕小组件、macOS 仪表板和今日面板小组件、Apple Touch Bar、Samsung Daily Cards、迷你应用小组件和智能手表应用助手。

在 Windows 11 上,小组件显示在小组件板中,可从任务栏左侧打开:

Windows 11 中的小组件板

在 Windows 11 中,渐进式Web 应用 (PWA) 可以定义小组件、更新它们以及处理其中的用户交互。

需要为 PWA 生成自定义小组件

现有 PWA 不能简单地按原样放置在小组件仪表板中,就像使用 Microsoft Edge 边栏一样。 相反,需要构建适合小组件主机(当前为 Windows 11 小组件板)的自定义小组件体验。 (将来可能会有其他小组件主机。) Windows 11 小组件板要求使用自适应卡片模板而不是 HTML 和 JavaScript 来生成小组件,因此必须将小组件与应用的其余 UI 分开设计。

另请参阅:

若要生成 PWA 驱动的小组件并通过 Microsoft Store 交付它,无需 C++/C# 代码。 生成小组件,并且可以从公共终结点成功安装和运行小组件后,可以使用 PWABuilder.com 打包应用并将应用发送到 Microsoft Store,而无需任何其他代码。 支持小组件的 PWA 必须可从公共终结点安装,因为 PWABuilder 不支持从 localhost.

另请参阅:

安装 WinAppSDK 并启用开发者模式

若要在本地计算机上启用开发和测试小组件,请执行以下操作:

  • 安装 WinAppSDK 1.2。

  • 在 Windows 11 中启用开发者模式:

    1. 打开设置。

    2. 在 查找设置 文本框中,输入 developer,然后单击 使用开发人员功能。

    3. 启用 开发人员模式:

      Windows 11 的开发人员设置

定义小组件

小组件是通过使用清单成员在 widgets PWA 清单文件中定义的。 此清单成员是可包含多个小组件定义的数组。

{
  "name": "PWAmp",
  "description": "A music player app",
  "icons": [
    { "src": "img/icon-96.png", "sizes": "96x96" },
    { "src": "img/icon-128.png", "sizes": "128x128" },
    { "src": "img/icon-256.png", "sizes": "256x256" },
    { "src": "img/icon-512.png", "sizes": "512x512" }
  ],
  "widgets": [
    /* widget definitions go here */
  ]
}

数组中的 widgets 每个条目都包含多个字段,如下所示:

{
  ...
  "widgets": [
    {
      "name": "PWAmp mini player",
      "description": "widget to control the PWAmp music player",
      "tag": "pwamp",
      "template": "pwamp-template",
      "ms_ac_template": "widgets/mini-player-template.json",
      "data": "widgets/mini-player-data.json",
      "type": "application/json",
      "screenshots": [
        {
          "src": "./screenshot-widget.png",
          "sizes": "600x400",
          "label": "The PWAmp mini-player widget"
        }
      ],
      "icons": [
        {
          "src": "./favicon-16.png",
          "sizes": "16x16"
        }
      ],
      "auth": false,
      "update": 86400
    }
  ]
}

在上面的示例中,音乐播放器应用程序定义了一个迷你播放器小组件。 Web 应用清单中的构件定义具有以下必填字段和可选字段:

字段 说明 是否必需?
name 提供给用户的小组件的标题。 是
short_name 名称的备用简写版本。 否
description 小组件功能的说明。 是
icons 要用于小组件的图标数组。 如果缺少, icons 则改用清单成员。 忽略大于 1024x1024 的图标。 否
screenshots 显示小组件外观的屏幕截图数组。 类似于 screenshot 清单成员。 platform屏幕截图项的字段支持 和Windowsany值。 忽略大于 1024x1024 像素的图像。 有关特定于 Windows 11 小组件板的屏幕截图要求,请参阅与小组件选取器集成中的屏幕截图图像要求。 是
tag 用于引用 PWA 服务工作线程中的小组件的字符串。 是
template 用于在操作系统小组件仪表板中显示小组件的模板。 注意:此属性当前仅供参考,未使用。 见 ms_ac_template 下文。 否
ms_ac_template 用于在操作系统小组件仪表板中显示小组件的自定义自适应卡模板的 URL。 请参阅下面的 定义小组件模板 。 是
data 可在其中找到用于填充模板的数据的 URL。 如果存在,则需要此 URL 才能返回有效的 JSON。 否
type 小组件数据的 MIME 类型。 否
auth 一个布尔值,指示小组件是否需要身份验证。 否
update 更新构件的频率(以秒为单位)。 服务工作进程中的代码必须执行更新;构件不会自动更新。 请参阅 在运行时访问构件实例。 否
multiple 一个布尔值,指示是否允许小组件的多个实例。 默认为 true。 否

定义小组件模板

为了使小部件易于创建和适应各种操作系统小部件仪表板,它们使用模板显示。 存在两种类型的模板:

  • 通用模板,由其名称使用字段 template 定义。
  • 自定义模板,由其 URL 使用自定义模板字段定义。

目前仅支持自定义自适应卡片模板。 自适应卡是一种开放的卡交换格式,可用于以常见且一致的方式交换 UI 内容。 请参阅 自适应卡概述。

若要在 Windows 11 上定义自定义自适应卡片模板,请使用 ms_ac_template Web 应用清单中的小组件定义中的字段。 尽管当前未使用,但它 template 是必填字段。

{
  ...
  "template": "pwamp-template",
  "ms_ac_template": "widgets/mini-player.json",
  ...
}

ms_ac_template字段值应为模板文件的有效 URL。

下面是自适应卡片模板的示例:

{
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "TextBlock",
      "size": "Medium",
      "text": "Now playing...",
      "horizontalAlignment": "Center"
    },
    {
      "type": "TextBlock",
      "spacing": "Large",
      "weight": "Bolder",
      "horizontalAlignment": "Center",
      "text": "${song}, by ${artist}",
    }
  ],
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.5"
}

若要了解详细信息,请参阅 自适应卡模板。

接下来,您需要将数据绑定到模板。

将数据绑定到模板

模板声明小组件的用户界面。 然后,数据将填充此用户界面。

要将数据绑定到模板,请使用 data 小组件定义中的字段。 应将此字段设置为返回有效 JSON 数据的 URL。

上一节中定义的模板包含两个变量:song和 artist,它们包含在绑定表达式语法中: ${}。 小组件定义中 URL 返回 data 的数据应包含这些变量的值。

下面是 URL 可能返回的内容 data 的示例:

{
  "song": "I Will Always Love You",
  "artist": "Whitney Houston"
}

定义小组件操作

如果希望小组件允许用户执行任务,请定义支持操作的模板。

下面是自定义自适应卡片模板中定义的操作示例:

{
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "TextBlock",
      "size": "Medium",
      "text": "Now playing...",
      "horizontalAlignment": "Center"
    },
    {
      "type": "TextBlock",
      "spacing": "Large",
      "weight": "Bolder",
      "horizontalAlignment": "Center",
      "text": "${song}, by ${artist}",
    }
  ],
  "actions": [
    {
      "type": "Action.Execute",
      "title": "Previous",
      "verb": "previous-song"
    },
    {
      "type": "Action.Execute",
      "title": "Next",
      "verb": "next-song"
    }
  ],
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.5"
}

请注意 verb 上述 JSON 模板中的字段。 在服务工作进程代码中处理小组件操作时,将使用它。 请参阅 处理小组件操作。

在运行时访问构件实例

可以通过 PWA 服务工作线程代码访问小组件并对其进行更新。 在运行时访问小组件在以下情况下非常有用:

服务工作进程有权访问 self.widgets 对象和多个小组件事件,这些事件共同构成一个 API,用于在运行时响应更改和访问小组件。

以下部分提供了代码示例。 有关 API 的参考,请参阅 服务工作线程 API 参考。

安装时呈现小组件

安装 PWA 后,应用在其清单中定义的小组件将添加到小组件仪表板中,但尚未安装。 仅当用户选择从仪表板添加小组件时,才会安装小组件。

安装构件时,不会使用构件定义的 and data 字段自动ms_ac_template呈现它。

若要呈现小组件,请侦听 widgetinstall 服务工作线程中的事件,并使用以下 widgets.updateByTag 函数更新小组件:

// Listen to the widgetinstall event.
self.addEventListener("widgetinstall", event => {
  // The widget just got installed, render it using renderWidget.
  // Pass the event.widget object to the function.
  event.waitUntil(renderWidget(event.widget));
});

async function renderWidget(widget) {
  // Get the template and data URLs from the widget definition.
  const templateUrl = widget.definition.msAcTemplate;
  const dataUrl = widget.definition.data;

  // Fetch the template text and data.
  const template = await (await fetch(templateUrl)).text();
  const data = await (await fetch(dataUrl)).text();

  // Render the widget with the template and data.
  await self.widgets.updateByTag(widget.definition.tag, {template, data});
}

在服务工作进程更新上更新小组件

当 PWA 中的服务工作进程代码发生更改时,浏览器会检测到该更改,安装新的服务工作进程,然后稍后激活该服务工作进程。

发生这种情况时,请务必更新可能已在运行的任何小组件实例。 在发出服务工作进程 activate 事件之前,可能已安装小组件。 要避免显示空小组件,请在事件发生时 activate 更新小组件

// Update the widgets to their initial states
// when the service worker is activated.
self.addEventListener("activate", event => {
  event.waitUntil(updateWidgets());
});

async function updateWidgets() {
  // Get the widget that match the tag defined in the web app manifest.
  const widget = await self.widgets.getByTag("pwamp");
  if (!widget) {
    return;
  }

  // Using the widget definition, get the template and data.
  const template = await (await fetch(widget.definition.msAcTemplate)).text();
  const data = await (await fetch(widget.definition.data)).text();

  // Render the widget with the template and data.
  await self.widgets.updateByTag(widget.definition.tag, {template, data});
}

处理小组件操作

如果小组件模板包含操作,则用户可以通过单击呈现的小组件中的按钮来运行这些操作。 有关如何在模板中定义操作的信息,请参阅 定义小组件操作。

当用户运行小组件操作时,会在 PWA 服务工作线程中触发一个 widgetclick 事件。 若要处理用户操作,请侦听事件:

self.addEventListener('widgetclick', (event) => {
  switch (event.action) {
    case 'previous-song':
      // Application logic to play the previous song...
      break;
    case 'next-song':
      // Application logic to play the next song...
      break;
  }
});

为了简洁起见,实际的应用程序代码未显示在上述代码段中。 收到 OR next-song 操作后previous-song,可能需要使用 Client.postMessage 向应用发送一条消息,让应用知道它应该开始播放上一首或下一首歌曲。

请注意, action 传递给上面事件侦听器的对象属性 widgetEvent 与小部件模板字段中 action.verb 定义的字符串匹配。

有关该事件以及可从中访问哪些信息的详细信息 widgetclick ,请参阅下面的 服务工作线程 API 参考。

在应用程序更改时更新小组件

在前面的部分中,您了解了如何在发生特定小组件事件、小组件操作和服务工作线程更新时更新小组件。 当应用程序中发生某些事情时,或者当推送通知发生时,或者定期更新小部件也很有用。

在本部分中,你将了解如何使用定期后台同步 API 定期更新小组件。 有关定期后台同步 API 的详细信息,请参阅使用 定期后台同步 API 定期获取新鲜内容。

在以下代码段中,事件侦听器用于响应应用程序小部件的各种生命周期事件。 当检测到小组件安装时,将注册定期同步,当检测到小组件删除时,将取消注册定期同步。

当定期发生同步事件时,将使用该 widgets.updateByTag 函数更新小组件实例。

self.addEventListener("widgetinstall", event => {
  event.waitUntil(onWidgetInstall(event.widget));
});

self.addEventListener("widgetuninstall", event => {
  event.waitUntil(onWidgetUninstall(event.widget));
});

async function onWidgetInstall(widget) {
  // Register a periodic sync, if this wasn't done already.
  // We use the same tag for the sync registration and the widget to
  // avoid registering several periodic syncs for the same widget.
  const tags = await self.registration.periodicSync.getTags();
  if (!tags.includes(widget.definition.tag)) {
    await self.registration.periodicSync.register(widget.definition.tag, {
      minInterval: widget.definition.update
    });
  }

  // And also update the instance.
  await updateWidget(widget);
}

async function onWidgetUninstall(widget) {
  // On uninstall, unregister the periodic sync.
  // If this was the last widget instance, then unregister the periodic sync.
  if (widget.instances.length === 1 && "update" in widget.definition) {
    await self.registration.periodicSync.unregister(widget.definition.tag);
  }
}

// Listen to periodicsync events to update all widget instances
// periodically.
self.addEventListener("periodicsync", async event => {
  const widget = await self.widgets.getByTag(event.tag);

  if (widget && "update" in widget.definition) {
    event.waitUntil(updateWidget(widget));
  }
});

async function updateWidget(widget) {
  // Get the template and data URLs from the widget definition.
  const templateUrl = widget.definition.msAcTemplate;
  const dataUrl = widget.definition.data;

  // Fetch the template text and data.
  const template = await (await fetch(templateUrl)).text();
  const data = await (await fetch(dataUrl)).text();

  // Render the widget with the template and data.
  await self.widgets.updateByTag(widget.definition.tag, {template, data});
}

演示应用

PWAmp 是一个定义小组件的音乐播放器 PWA 演示应用程序。 PWAmp 小组件允许用户可视化当前歌曲并播放上一首或下一首歌曲。

  1. 如果尚未完成,请安装 WinAppSDK 1.2 并在 Windows 11 中启用开发人员模式。

  2. 转到 PWAmp 并在 Windows 11 上安装该应用。

  3. 按 Windows 徽标键 + W 打开 Windows 11 小组件板。

  4. 单击 “添加小组件” 打开 小组件设置 屏幕,滚动到 PWAmp 迷你播放器 小组件并添加它。

  5. 关闭 小组件设置 屏幕。 现在, PWAmp 迷你播放器 显示在小组件板中。

PWAmp 小组件显示当前歌曲和用于播放上一首或下一首歌曲的按钮。

Windows 小组件板,PWAmp 演示应用旁边。小组件板包含 PWAmp 迷你播放器小组件,显示当前在 PWAmp 应用中播放的歌曲

服务工作线程 API 参考

Service Worker 全局对象 (或 ServiceWorkerGlobalScope) 包含一个 widgets 属性,该属性公开以下基于 Promise 的方法:

方法 说明 参数 返回值
getByTag(tag) 按标记获取小组件。 小组件标记 解析为与标记匹配的小 部件对象 的 Promise,或 undefined。
getByInstanceId(id) 按实例 ID 获取小组件。 小组件实例 ID 解析为相应小 部件对象的 Promise 或 undefined.
getByHostId(id) 按主机 ID 获取小组件。 主机 ID 在该主机中找到的小 组件对象 数组。
matchAll(options) 通过匹配选项获取小组件。 widgetOptions 对象 解析为与条件匹配options的小部件对象数组的 Promise。
updateByInstanceId(id, payload) 按实例 ID 汇报小组件。 实例 ID 和 widgetPayload 对象 解析为 undefined 或 Error的 Promise。
updateByTag(tag, payload) 按标记汇报小组件。 构件标记和 widgetPayload 对象 解析为 undefined 或 Error的 Promise。

服务工作线程全局对象还定义以下事件:

  • widgetinstall:在构件主机安装构件时触发。
  • widgetuninstall:在构件主机卸载构件时触发。
  • widgetresume:当小组件主机恢复渲染已安装的小组件时触发,这可能发生在主机暂停小组件渲染以保留资源后。
  • widgetclick:当用户运行其中一个小组件操作时触发。

有关随这些事件提供的对象的更多信息,请参阅下面的 widgetEvent 对象 和 widgetClickEvent 对象。

小组件对象

每个小部件都表示为一个 widget 对象,其中包含以下属性:

widgetOptions 对象

当用于 matchAll(options) 获取多个小部件时,需要一个 widgetOptions 对象来过滤要返回的小部件。 widgetOptions对象包含以下属性,所有这些属性都是可选的:

  • installable:指示返回的小组件是否应可安装的布尔值。
  • installed:一个布尔值,指示返回的小组件是否已安装在小组件主机中。
  • tag:用于按标记筛选返回的小组件的字符串。
  • instanceId:用于按实例 ID 筛选返回的小组件的字符串。
  • hostId:用于按小组件主机 ID 筛选返回的小组件的字符串。

widgetPayload 对象

创建或更新小组件实例时,服务工作进程必须发送填充小组件所需的模板和数据。 模板和数据称为 有效负载。 widgetPayload该对象包含以下属性:

  • template:用于呈现小组件的模板,作为字符串。 这将是自适应卡模板的字符串化 JSON。
  • data:要与小组件模板一起使用的字符串数据。 此数据可以是字符串化的 JSON 数据。

widgetInstance 对象

此对象表示小组件主机中小组件的给定实例,并包含以下属性:

  • id:用于引用实例的内部 GUID 字符串。
  • host:指向已安装此实例的小组件主机的内部指针。
  • updated: Date 表示上次将数据发送到实例的时间的对象。
  • payload:表示发送到此实例的最后一个有效负载的 widgetPayload 对象 。

widgetDefinition 对象

此对象表示在 PWA 清单文件中找到的小组件的原始定义。 此对象的属性与上面 的“定义”小组件中列出的属性匹配。

widgetEvent 对象

此对象作为参数传递给 、 widgetuninstall和 widgetresume类型的widgetinstall服务工作线程小部件事件的侦听器。

对于 widgetinstall、 和widgetuninstallwidgetresume事件类型,widgetEvent对象具有以下属性:

属性 说明 类型
widget 触发事件的小组件实例。 小组件
instanceId 小组件实例 ID。 String
hostId 小组件主机 ID。 String

widgetClickEvent 对象

此对象作为参数传递给类型为 widgetclick. 的服务工作线程小部件事件的侦听器 。 可以通过使用 clients.openWindow()打开应用的窗口来响应事件。widgetclick

对象 widgetClickEvent 具有以下属性:

属性 说明 类型
action 触发事件的操作,如小组件模板的字段中 actions.verb 所定义。 请参阅 定义小组件操作。 String
widget 触发事件的小组件实例。 widgetInstance
hostId 小组件主机 ID。 String
instanceId 小组件实例 ID。 String