首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

Windows 8 開發入門(十一):Toast、Tile、Badge 三種通知機制完整實作

     編輯:關於C#

Windows 8 提供三種推播通知管道,適用不同情境:

  • Toast:即時彈出提醒(應用內或系統級)
  • Tile:磚塊動態更新(開始畫面應用圖示)
  • Badge:磚塊右下角數字/圖示(訊息計數、播放狀態)

前置準備

  1. 參考 NotificationsExtensions.winmd(簡化 XML 模板操作)
  2. Package.appxmanifest 設定各尺寸徽標
  3. Package.appxmanifest → 應用程式 UI:支援 Toast 通知 = 是
Toast 通知啟用設定
圖 1:啟用 Toast 通知

Toast 通知:文字 + 圖片

private void ToastNotice_Click(object sender, RoutedEventArgs e) {
    var toast = ToastContentFactory.CreateToastImageAndText01();
    toast.TextBodyWrap.Text = "今日世界末日倒數 10 天!";
    toast.Image.Src = "https://news.shangdu.com/301/20120512/P_5626361_0__1686841290.jpg";
    ToastNotificationManager.CreateToastNotifier().Show(toast.CreateNotification());
}
Toast 彈出效果
圖 2:Toast 通知執行畫面

Tile 磚塊:寬磚 + 方磚雙尺寸支援

private void TileNotice_Click(object sender, RoutedEventArgs e) {
    // 寬磚模板
    var wideTile = TileContentFactory.CreateTileWideImageAndText01();
    wideTile.TextCaptionWrap.Text = "小資情有獨鐘 10 款合資熱銷時尚車型導購";
    wideTile.Image.Src = "https://news.mycar168.com/uploadfile/2011/1030/20111030040816628.jpg";

    // 方磚備用內容
    var squareTile = TileContentFactory.CreateTileSquareImage();
    squareTile.Image.Src = "https://news.mycar168.com/uploadfile/2011/1030/20111030040816628.jpg";
    wideTile.SquareContent = squareTile;

    TileUpdateManager.CreateTileUpdaterForApplication().Update(wideTile.CreateNotification());
}

private void ClearTile_Click(object sender, RoutedEventArgs e) {
    TileUpdateManager.CreateTileUpdaterForApplication().Clear();
}
Tile 磚塊更新效果
圖 3:Tile 寬磚與方磚同時更新

Badge:數字計數與狀態圖示

// 數字 Badge (1-99)
private void BadgeNotice_Click(object sender, RoutedEventArgs e) {
    var badge = new BadgeNumericNotificationContent(29);
    BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge.CreateNotification());
}

// 圖示 Badge (播放/暫停/新訊息等)
private void BadgeImage_Click(object sender, RoutedEventArgs e) {
    var badge = new BadgeGlyphNotificationContent(GlyphValue.Paused);
    BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge.CreateNotification());
}

private void BadgeClear_Click(object sender, RoutedEventArgs e) {
    BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();
}
Badge 數字效果
圖 4:Badge 數字 29 顯示於磚塊右下角
Badge 圖示效果
圖 5:Badge 暫停圖示

前端按鈕佈局

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Button Name="ToastNotice" Content="Toast 通知" Click="ToastNotice_Click" .../>
    <Button Name="TileNotice" Content="Tile 通知" Click="TileNotice_Click" .../>
    <Button Name="ClearTile" Content="清除 Tile" Click="ClearTile_Click" .../>
    <Button Name="BadgeNotice" Content="Badge 數字" Click="BadgeNotice_Click" .../>
    <Button Name="BadgeImage" Content="Badge 圖示" Click="BadgeImage_Click" .../>
    <Button Name="BadgeClear" Content="Badge 清除" Click="BadgeClear_Click" .../>
</Grid>

原始碼下載:Win8Notice.rar

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved