Windows 8 將鎖畫面開放給應用程式顯示徽章、詳細文字甚至自訂背景圖片,讓使用者在不解鎖裝置的情況下即時掌握資訊。本文詳細說明如何申請鎖畫面權限、發送通知、支援多重磚貼綁定與自訂鎖畫面背景。
應用程式必須宣告背景工作支援「推播通知」或「控制通道」,否則 RequestAccessAsync() 會拋出例外。
// 請求權限(會彈出系統對話框)
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;
}
鎖畫面整合包含四個層次:權限申請 → 徽章通知 → 文字通知 → 多磚貼綁定 → 背景自訂。開發者應根據應用程式屬性選擇適當層次,避免過度干擾使用者。