重新想象 Windows 8 Store Apps (59) - 锁屏
生活随笔
收集整理的這篇文章主要介紹了
重新想象 Windows 8 Store Apps (59) - 锁屏
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:重新想象 Windows 8 Store Apps (59) - 鎖屏
[源碼下載]
作者:webabcd
介紹
重新想象 Windows 8 Store Apps 之?鎖屏
- 登錄鎖屏,獲取當(dāng)前程序的鎖屏權(quán)限,從鎖屏中移除
- 發(fā)送徽章或文本到鎖屏
- 將一個(gè) app 的多個(gè) tile 綁定到鎖屏
- 自定義鎖屏圖片
示例
1、演示如何登錄鎖屏,獲取當(dāng)前程序的鎖屏權(quán)限,從鎖屏中移除
LockScreen/AccessLockScreen.xaml
LockScreen/AccessLockScreen.xaml.cs
/** 演示如何登錄鎖屏,獲取當(dāng)前程序的鎖屏權(quán)限,從鎖屏中移除* * 注:* 要想請(qǐng)求鎖屏權(quán)限,需要后臺(tái)任務(wù)支持“推送通知”或“控制通道”*/using System; using Windows.ApplicationModel.Background; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls;namespace XamlDemo.LockScreen {public sealed partial class AccessLockScreen : Page{public AccessLockScreen(){this.InitializeComponent();}private async void btnRequestAccess_Click(object sender, RoutedEventArgs e){try{// 向系統(tǒng)請(qǐng)求登錄鎖屏,會(huì)彈出確認(rèn)對(duì)話框// 需要后臺(tái)任務(wù)支持“推送通知”或“控制通道”,否則會(huì)拋出異常// 不能在模擬器中運(yùn)行// 如果 BackgroundAccessStatus 不等于 Unspecified,則即使調(diào)用 RequestAccessAsync() 也不會(huì)出現(xiàn)對(duì)話框,需要用戶去“設(shè)置”中去添加或移除鎖屏應(yīng)用BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();/** BackgroundAccessStatus - 當(dāng)前 app 的鎖屏權(quán)限* Unspecified - 用戶尚未選擇* Denied - 被用戶拒絕* AllowedWithAlwaysOnRealTimeConnectivity - 用于允許了,且支持實(shí)時(shí)連接,即使電量低* AllowedMayUseActiveRealTimeConnectivity - 用于允許了,且支持實(shí)時(shí)連接,但是如果電量低則無(wú)法實(shí)時(shí)連接*/lblMsg.Text = "RequestAccessAsync(): " + status.ToString();}catch (Exception ex){lblMsg.Text = ex.ToString();}}private void btnGetAccessStatus_Click(object sender, RoutedEventArgs e){try{// 獲取當(dāng)前應(yīng)用程序的鎖屏權(quán)限BackgroundAccessStatus status = BackgroundExecutionManager.GetAccessStatus();lblMsg.Text = "GetAccessStatus(): " + status.ToString();}catch (Exception ex){lblMsg.Text = ex.ToString();}}private void btnRemoveAccess_Click(object sender, RoutedEventArgs e){try{// 將當(dāng)前應(yīng)用程序從鎖屏中移除 BackgroundExecutionManager.RemoveAccess();lblMsg.Text = "RemoveAccess()";}catch (Exception ex){lblMsg.Text = ex.ToString();}}} }
2、演示如何發(fā)送徽章或文本到鎖屏
LockScreen/SendNotification.xaml
LockScreen/SendNotification.xaml.cs
/** 演示如何發(fā)送徽章或文本到鎖屏* * 注:* 如果需要發(fā)送文本到鎖屏,需要手動(dòng)在“設(shè)置”中將 app 添加到“選擇要顯示詳細(xì)狀態(tài)的應(yīng)用”中* * 另:* 關(guān)于 tile 和 badge 請(qǐng)參見(jiàn):XamlDemo/Tile*/using NotificationsExtensions.BadgeContent; using NotificationsExtensions.TileContent; using Windows.UI.Notifications; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls;namespace XamlDemo.LockScreen {public sealed partial class SendNotification : Page{public SendNotification(){this.InitializeComponent();}private void btnSendBadge_Click(object sender, RoutedEventArgs e){// 發(fā)送 badge 到鎖屏BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent(3);BadgeNotification badge = badgeContent.CreateNotification();BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();badgeUpdater.Update(badge);}private void btnSendTile_Click(object sender, RoutedEventArgs e){// 發(fā)送文本到鎖屏,前提是此 app 在“選擇要顯示詳細(xì)狀態(tài)的應(yīng)用”中ITileWideSmallImageAndText03 tileContent = TileContentFactory.CreateTileWideSmallImageAndText03();tileContent.TextBodyWrap.Text = "hello webabcd";tileContent.Image.Src = "ms-appx:///Assets/Logo.png";tileContent.RequireSquareContent = false;TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());}} }
3、演示如何將一個(gè) app 的多個(gè) tile 綁定到鎖屏
LockScreen/MultipleTiles.xaml
LockScreen/MultipleTiles.xaml.cs
/** 演示如何將一個(gè) app 的多個(gè) tile 綁定到鎖屏* * 要想將 SecondaryTile 綁定到鎖屏,需要注意:* 1、需要設(shè)置 SecondaryTile 的 LockScreenBadgeLogo* 2、如果需要文本支持則還需要設(shè)置 SecondaryTile 的 LockScreenDisplayBadgeAndTileText 為 true* 3、需要手動(dòng)在“設(shè)置”中將 SecondaryTile 添加到鎖屏,當(dāng)然如果需要文本支持則需要手動(dòng)將 app 添加到“選擇要顯示詳細(xì)狀態(tài)的應(yīng)用”中*/using NotificationsExtensions.BadgeContent; using NotificationsExtensions.TileContent; using System; using Windows.UI.Notifications; using Windows.UI.Popups; using Windows.UI.StartScreen; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using XamlDemo.Common;namespace XamlDemo.LockScreen {public sealed partial class MultipleTiles : Page{private string _tile1Id = "123";private string _tile2Id = "456";public MultipleTiles(){this.InitializeComponent();}// 僅支持 badge 的可以登錄鎖屏的 SecondaryTileprivate async void btnOnlyBadge_Click(object sender, RoutedEventArgs e){SecondaryTile secondTile = new SecondaryTile(_tile1Id,"testOnlyBadge","testOnlyBadge","argument1",TileOptions.ShowNameOnLogo,new Uri("ms-appx:///Assets/Logo.png"));// 需要指定 LockScreenBadgeLogosecondTile.LockScreenBadgeLogo = new Uri("ms-appx:///Assets/BadgeLogo.png");bool isPinned = await secondTile.RequestCreateForSelectionAsync(Helper.GetElementRect((FrameworkElement)sender), Placement.Above);if (isPinned){BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent(2);BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(_tile1Id).Update(badgeContent.CreateNotification());}}// 即支持徽章又支持文本的可以登錄鎖屏的 SecondaryTileprivate async void btnBadgeAndText_Click(object sender, RoutedEventArgs e){SecondaryTile secondTile = new SecondaryTile(_tile2Id,"testBadgeAndText","testBadgeAndText","argument2",TileOptions.ShowNameOnLogo | TileOptions.ShowNameOnWideLogo,new Uri("ms-appx:///Assets/Logo.png"),new Uri("ms-appx:///Assets/WideLogo.png"));// 需要指定 LockScreenBadgeLogosecondTile.LockScreenBadgeLogo = new Uri("ms-appx:///Assets/BadgeLogo.png");// 需要設(shè)置 LockScreenDisplayBadgeAndTileText 為 truesecondTile.LockScreenDisplayBadgeAndTileText = true;bool isPinned = await secondTile.RequestCreateForSelectionAsync(Helper.GetElementRect((FrameworkElement)sender), Placement.Above);if (isPinned){ITileWideText03 tileContent = TileContentFactory.CreateTileWideText03();tileContent.TextHeadingWrap.Text = "hello webabcd";tileContent.RequireSquareContent = false;TileUpdateManager.CreateTileUpdaterForSecondaryTile(_tile2Id).Update(tileContent.CreateNotification());}}} }
4、演示如何自定義鎖屏圖片
LockScreen/CustomLockScreenImage.xaml
LockScreen/CustomLockScreenImage.xaml.cs
/** 演示如何自定義鎖屏圖片*/using System; using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; using XamlDemo.Common;namespace XamlDemo.LockScreen {public sealed partial class CustomLockScreenImage : Page{public CustomLockScreenImage(){this.InitializeComponent();}private async void btnDemo_Click(object sender, RoutedEventArgs e){if (Helper.EnsureUnsnapped()){FileOpenPicker imagePicker = new FileOpenPicker{ViewMode = PickerViewMode.Thumbnail,SuggestedStartLocation = PickerLocationId.PicturesLibrary,FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" }};StorageFile imageFile = await imagePicker.PickSingleFileAsync();if (imageFile != null){// 將指定的圖片設(shè)置為鎖屏圖片await Windows.System.UserProfile.LockScreen.SetImageFileAsync(imageFile);// 獲取當(dāng)前的鎖屏圖片IRandomAccessStream imageStream = Windows.System.UserProfile.LockScreen.GetImageStream();if (imageStream != null){BitmapImage lockScreenImage = new BitmapImage();lockScreenImage.SetSource(imageStream);img.Source = lockScreenImage;}}}}} }
OK
[源碼下載]
總結(jié)
以上是生活随笔為你收集整理的重新想象 Windows 8 Store Apps (59) - 锁屏的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: word制作一个申请表
- 下一篇: 【原创】构建高性能ASP.NET站点 开