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

Windows 8 市集應用程式開發實戰 (59):鎖畫面整合與自訂

     編輯:關於.NET

前言:鎖畫面是應用程式的第二入口

Windows 8 將鎖畫面開放給應用程式顯示徽章、詳細文字甚至自訂背景圖片,讓使用者在不解鎖裝置的情況下即時掌握資訊。本文詳細說明如何申請鎖畫面權限、發送通知、支援多重磚貼綁定與自訂鎖畫面背景。

一、鎖畫面存取權限管理

必要條件

應用程式必須宣告背景工作支援「推播通知」或「控制通道」,否則 RequestAccessAsync() 會拋出例外。

核心 API

// 請求權限(會彈出系統對話框)
BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
// 取得目前權限
BackgroundAccessStatus current = BackgroundExecutionManager.GetAccessStatus();
// 移除權限
BackgroundExecutionManager.RemoveAccess();

權限狀態枚舉

  • Unspecified:使用者尚未選擇
  • Denied:使用者拒絕
  • AllowedWithAlwaysOnRealTimeConnectivity:允許且支援低電量下的即時連線
  • AllowedMayUseActiveRealTimeConnectivity:允許但低電量時可能中斷即時連線

二、發送徽章與文字通知到鎖畫面

徽章通知

使用 NotificationsExtensions.BadgeContent 建立數字徽章:

BadgeNumericNotificationContent badge = new BadgeNumericNotificationContent(3);
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge.CreateNotification());

詳細文字通知

需使用者在「設定」→「鎖畫面」→「選擇要顯示詳細狀態的應用程式」中手動加入:

ITileWideSmallImageAndText03 tile = TileContentFactory.CreateTileWideSmallImageAndText03();
tile.TextBodyWrap.Text = "hello webabcd";
tile.Image.Src = "ms-appx:///Assets/Logo.png";
tile.RequireSquareContent = false;
TileUpdateManager.CreateTileUpdaterForApplication().Update(tile.CreateNotification());

三、多重磚貼綁定鎖畫面

透過 SecondaryTile 讓單一應用程式在鎖畫面顯示多個獨立資訊來源:

僅支援徽章的次要磚貼

SecondaryTile tile = new SecondaryTile(
    "123", "testOnlyBadge", "testOnlyBadge", "argument1",
    TileOptions.ShowNameOnLogo, new Uri("ms-appx:///Assets/Logo.png"));
tile.LockScreenBadgeLogo = new Uri("ms-appx:///Assets/BadgeLogo.png"); // 必須指定
bool pinned = await tile.RequestCreateForSelectionAsync(rect, Placement.Above);
if (pinned) {
    BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile("123")
        .Update(new BadgeNumericNotificationContent(2).CreateNotification());
}

同時支援徽章與文字的次要磚貼

SecondaryTile tile = new SecondaryTile(
    "456", "testBadgeAndText", "testBadgeAndText", "argument2",
    TileOptions.ShowNameOnLogo | TileOptions.ShowNameOnWideLogo,
    new Uri("ms-appx:///Assets/Logo.png"), new Uri("ms-appx:///Assets/WideLogo.png"));
tile.LockScreenBadgeLogo = new Uri("ms-appx:///Assets/BadgeLogo.png");
tile.LockScreenDisplayBadgeAndTileText = true; // 關鍵屬性
bool pinned = await tile.RequestCreateForSelectionAsync(rect, Placement.Above);
if (pinned) {
    TileUpdateManager.CreateTileUpdaterForSecondaryTile("456")
        .Update(TileContentFactory.CreateTileWideText03().CreateNotification());
}

注意:使用者仍需在系統設定中手動將次要磚貼加入鎖畫面,文字內容更需加入「詳細狀態」清單。

四、自訂鎖畫面背景圖片

選取圖片並套用

FileOpenPicker picker = new FileOpenPicker {
    ViewMode = PickerViewMode.Thumbnail,
    SuggestedStartLocation = PickerLocationId.PicturesLibrary,
    FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" }
};
StorageFile file = await picker.PickSingleFileAsync();
if (file != null) {
    await Windows.System.UserProfile.LockScreen.SetImageFileAsync(file);
    // 即時預覽
    IRandomAccessStream stream = Windows.System.UserProfile.LockScreen.GetImageStream();
    BitmapImage bitmap = new BitmapImage();
    bitmap.SetSource(stream);
    imgPreview.Source = bitmap;
}

總結與原始碼下載

鎖畫面整合包含四個層次:權限申請 → 徽章通知 → 文字通知 → 多磚貼綁定 → 背景自訂。開發者應根據應用程式屬性選擇適當層次,避免過度干擾使用者。

完整原始碼下載: https://files.cnblogs.com/webabcd/Windows8.rar

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