通知徽章會傳達應用程式專屬的摘要或狀態資訊。 它們可以是數值 (1-99) 或系統提供的字符集。 透過徽章傳達的最佳訊息範例包括線上遊戲中的網路連線狀態、訊息應用程式中的使用者狀態、郵件應用程式中的未讀郵件數量以及社群媒體應用程式中的新貼文數量。
無論應用程式是否正在執行,通知徽章都會顯示在應用程式的工作列圖示上及其開始磚的右下角。 可以在所有磁磚大小上顯示徽章。
Note
您無法提供自己的徽章影像; 只能使用系統提供的徽章影像。
數字徽章
| Value | Badge | XML |
|---|---|---|
| 1 至 99 之間的數字。 數值為 0 相當於字形值「none」並會清除徽章。 |
|
<badge value="1"/> |
| 任何大於 99 的數字。 |
|
<badge value="100"/> |
符號徽章
徽章可以顯示一組不可延伸的狀態圖標,這是取代數字的選項。
| Status | Glyph | XML |
|---|---|---|
| none | (未顯示任何徽章。) | <badge value="none"/> |
| 活動 |
|
<badge value="activity"/> |
| alarm |
|
<badge value="alarm"/> |
| 警示 |
|
<badge value="alert"/> |
| attention |
|
<badge value="attention"/> |
| available |
|
<badge value="available"/> |
| away |
|
<badge value="away"/> |
| busy |
|
<badge value="busy"/> |
| 錯誤 |
|
<badge value="error"/> |
| newMessage |
|
<badge value="newMessage"/> |
| paused |
|
<badge value="paused"/> |
| playing |
|
<badge value="playing"/> |
| unavailable |
|
<badge value="unavailable"/> |
在 Windows 應用程式 SDK 應用程式中設定徽章
從 Windows 應用程式 SDK 1.7 開始,在 Microsoft.Windows.BadgeNotifications 命名空間中使用 BadgeNotificationManager 類別來設定和清除徽章。 這是 Windows 應用程式 SDK 應用程式的推薦做法。 你不是建立 XML 有效載荷,而是呼叫一個方法並傳遞一個數字或 BadgeNotificationGlyph 值。
![]()
WinUI 3 圖庫應用程式包含互動式的 WinUI 控制項與功能範例。 你可以從 Microsoft Store 下載應用程式,或瀏覽 GitHub 的原始碼。
Important
BadgeNotificationManager 需要套件識別。 如果你的應用程式沒有套件身分識別,例如因為它是未封裝的,呼叫 SetBadgeAsCount、SetBadgeAsGlyph 和 ClearBadge 時會擲回例外狀況。 欲了解更多資訊,請參閱《Windows 應用程式中套件識別碼概覽》。
徽章會出現在你應用程式的工作列圖示上。 如果應用程式釘選在工作列,即使應用程式未執行,通知徽章仍會顯示。 要取得應用程式的 BadgeNotificationManager 實例,請使用 「Current 屬性」。
以下範例基於 WinUI 3 圖庫範例。 要使用它們,請在你的程式碼檔案中加入 using Microsoft.Windows.BadgeNotifications; 指令。
設定數字徽記
呼叫 SetBadgeAsCount,並傳入要顯示的數字。 若數字大於99,徽章上會顯示「99+」。
private void SetBadgeCountButton_Click(object sender, RoutedEventArgs e)
{
// Show the number 5 on the app's taskbar icon.
BadgeNotificationManager.Current.SetBadgeAsCount(5);
}
設置一個字形徽章
呼叫 SetBadgeAsGlyph 並傳遞一個 BadgeNotificationGlyph 值。 列舉值對應於 Glyph 徽章表中的字形。
private void SetBadgeGlyphButton_Click(object sender, RoutedEventArgs e)
{
// Show the alert glyph on the app's taskbar icon.
BadgeNotificationManager.Current.SetBadgeAsGlyph(BadgeNotificationGlyph.Alert);
}
清除通知標記
呼叫 ClearBadge 以移除徽章。 將計數設為 0,或設為 BadgeNotificationGlyph.None 字形,也會清除徽章。
private void ClearBadgeButton_Click(object sender, RoutedEventArgs e)
{
BadgeNotificationManager.Current.ClearBadge();
}
使用 UWP BadgeUpdateManager API 建立徽章
這些範例使用了 Windows 執行階段 命名空間中的 Windows.UI.Notifications 類別,它是 UWP 應用程式的徽章 API。 你用前面表格的值來將徽章建構成 XML 酬載。 如果你的應用程式使用 Windows 應用程式 SDK 1.7 或更新版本,請改用 BadgeNotificationManager。
建立數值徽章
private void setBadgeNumber(int num)
{
// Get the blank badge XML payload for a badge number
XmlDocument badgeXml =
BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);
// Set the value of the badge in the XML to our number
XmlElement badgeElement = badgeXml.SelectSingleNode("/badge") as XmlElement;
badgeElement.SetAttribute("value", num.ToString());
// Create the badge notification
BadgeNotification badge = new BadgeNotification(badgeXml);
// Create the badge updater for the application
BadgeUpdater badgeUpdater =
BadgeUpdateManager.CreateBadgeUpdaterForApplication();
// And update the badge
badgeUpdater.Update(badge);
}
建立字符徽章
private void updateBadgeGlyph()
{
string badgeGlyphValue = "alert";
// Get the blank badge XML payload for a badge glyph
XmlDocument badgeXml =
BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeGlyph);
// Set the value of the badge in the XML to our glyph value
Windows.Data.Xml.Dom.XmlElement badgeElement =
badgeXml.SelectSingleNode("/badge") as Windows.Data.Xml.Dom.XmlElement;
badgeElement.SetAttribute("value", badgeGlyphValue);
// Create the badge notification
BadgeNotification badge = new BadgeNotification(badgeXml);
// Create the badge updater for the application
BadgeUpdater badgeUpdater =
BadgeUpdateManager.CreateBadgeUpdaterForApplication();
// And update the badge
badgeUpdater.Update(badge);
}
清除徽章
private void clearBadge()
{
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();
}
取得範例程式碼
-
Windows 應用程式 SDK 通知範例
說明如何發送應用程式通知和徽章更新。