Windows Phone 8 在鎖屏背景圖片是支持應用自定義的,並且在屏幕下方還支持應用通知提醒,這是一個十分吸引眼球的新功能 雖說目前已經看到很多應用已經做個了個特性今天我還是在這個里為大家相信說明一下 為后面想做這個功能的同學先鋪鋪路。
此文是 升級到WP8必需知道的13個特性 系列的一個更新 希望這個系列可以給 Windows Phone 8開發者帶來一些開發上的便利。
同時歡迎大家在這里和我溝通交流或者在新浪微博上 @王博_Nick
1. 鎖屏背景
正如我說windows phone 8 是支持鎖屏背景的替換的 下圖是摘自MSDN的一張原圖很好理解
代碼寫起來十分的簡單
首先還是在WMAppManifest文件中聲明下 這段XML要緊跟在<Tokens>節點后面
<Extensions> <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> </Extensions>
修改鎖屏背景代碼
這里我解釋一下 "ms-appx:///" 和 "ms-appdata:///Local/"
ms-appdata points to the root of the local app data folder.也就是說當你的圖片文件是在文件系統中的時候使用ms-appdata前綴,時常從網絡下載的圖片保存在隔離存儲器中就要使用這個前綴了。
ms-appx points to the Local app install folder, to reference resources bundled in the XAP package. 當此張圖片是和當前應用一起打包在XAP包中的時候使用ms-appx前綴。
private async void LockHelper(string filePathOfTheImage, bool isAppResource) { try { var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication; if (!isProvider) { // If you're not the provider, this call will prompt the user for permission. // Calling RequestAccessAsync from a background agent is not allowed. var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync(); // Only do further work if the access was granted. isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted; } if (isProvider) { // At this stage, the app is the active lock screen background provider. // The following code example shows the new URI schema. // ms-appdata points to the root of the local app data folder. // ms-appx points to the Local app install folder, to reference resources bundled in the XAP package. var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/"; var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute); // Set the lock screen background image. Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri); // Get the URI of the lock screen background image. var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri(); System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString()); } else { MessageBox.Show("You said no, so I can't update your background."); } } catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
經過我的測試執行到這里 LockScreenManager.RequestAccessAsync() 會彈出一個用戶提示 需要用戶確認。
// Setup lockscreen. if (!LockScreenManager.IsProvidedByCurrentApplication) { await LockScreenManager.RequestAccessAsync(); }
當你在更新你的鎖屏背景時 尤其是從獨立存儲空間中讀取時 請盡量避免相同的文件名經我測試相同文件名有可能會造成系統默認緩存導致圖片更新延遲的情況發生。
MSDN也提供了一個替換名稱的方法
string fileName; var currentImage = LockScreen.GetImageUri(); if (currentImage.ToString().EndsWith("_A.jpg")) { fileName = "LiveLockBackground_B.jpg"; } else { fileName = "LiveLockBackground_A.jpg"; } var lockImage = string.Format("{0}", fileName); // At this point in the code, write the image to isolated storage.
當然在運行程序之前我們不能用代碼干預設置鎖屏背景在我們的程序中可以先預設一張圖片作為鎖屏背景 但是這張圖片的命名必須是 DefaultLockScreen.jpg且將這張圖片放置在項目根目錄下.
同時在鎖屏設置頁面我們可以看到 open app的按鈕可以直接導航到我們的應用中去 這里處理這個導航的方法和App to App 的方法類似 重載在App中處理InitializePhoneApplication方法 UriMapperBase即可 相信參考 windows phone 8 中的應用間通信
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); string lockscreenKey = "WallpaperSettings"; string lockscreenValue = "0"; bool lockscreenValueExists = NavigationContext.QueryString.TryGetValue(lockscreenKey, out lockscreenValue); if (lockscreenValueExists) { // Navigate the user to your app's lock screen settings screen here, // or indicate that the lock screen background image is updating. } }
當然在應用中也可以通過 LaunchUriAsync 方法打開設置頁面 相信參考:http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662937(v=vs.105).aspx
private async void btnGoToLockSettings_Click(object sender, RoutedEventArgs e) { // Launch URI for the lock screen settings screen. var op = await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-lock:")); }
2. 鎖屏通知
windows phone 7 中已經包含了推送通知,在windows phone 8中開發者可以將其融入到鎖屏界面中來
下圖還是一張選自MSDN的截圖很清晰的說明通知的情況,左側大字是顯示的應用的詳細狀態,下面一行可以顯示5個應用程序的通知數量。
同樣在鎖屏設置中可以選擇設置顯示通知的應用及顯示即時狀態的應用。
下面介紹下應用如何實現這個通知
首先第一步還是要在WMAppManifest中聲明我們的應用是一個支持鎖屏通知的應用同時指定通知的圖標來源。
圖標必須是一個白色背景透明 38 x 38 像素的PNG 圖片。
在Token節點中
<DeviceLockImageURI IsRelative="true" IsResource="false">Assets\LockImage.png</DeviceLockImageURI>
其次在聲明支持通知 前者是聲明支持顯示應用顯示即時狀態 后者是聲明顯示應用顯示詳細狀態。
<Extensions> <Extension ExtensionName="LockScreen_Notification_IconCount" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> <Extension ExtensionName="LockScreen_Notification_TextField" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> </Extensions>
其實就這么簡單 只要你的應用之前支持推送你 那么經過以上的設置推送信息就會顯示在鎖屏界面上了。
同時歡迎大家在這里和我溝通交流或者在新浪微博上 @王博_Nick