Windows Phone 8 鎖屏背景與通知

Windows Phone 8 在鎖屏背景圖片是支持應用自定義的,而且在屏幕下方還支持應用通知提醒,這是一個十分吸引眼球的新功能 雖然說目前已經看到不少應用已經作個了個特性今天我仍是在這個裏爲你們相信說明一下 爲後面想作這個功能的同窗先鋪鋪路。windows

此文是升級到Windows Phone 8必需知道的13個特性系列的一個更新 但願這個系列能夠給 Windows Phone 8開發者帶來一些開發上的便利。緩存

1. 鎖屏背景網絡

正如我說windows phone 8 是支持鎖屏背景的替換的 下圖是摘自MSDN的一張原圖很好理解app

代碼寫起來十分的簡單async

首先仍是在WMAppManifest文件中聲明下 這段XML要緊跟在<Tokens>節點後面ide

<Extensions>     測試

<Extension ExtensionName="LockScreen_Background"this

ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}"spa

TaskID="_default" /></Extensions>code

修改鎖屏背景代碼

這裏我解釋一下"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.Exceptionex)    

{

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>

若是還有困惑的話IT在線教育平臺還有幾個類似的例子。不過我想各位看官應該已經很清楚了。其實就這麼簡單,只要你的應用以前支持推送你 那麼通過以上的設置推送信息就會顯示在鎖屏界面上了。

大功告成。

相關文章
相關標籤/搜索