天天看點

Windows Phone 8 鎖屏背景與通知

Windows Phone 8 在鎖屏背景圖檔是支援應用自定義的,并且在螢幕下方還支援應用通知提醒,這是一個十分吸引眼球的新功能 雖說目前已經看到很多應用已經做個了個特性今天我還是在這個裡為大家相信說明一下 為後面想做這個功能的同學先鋪鋪路。

此文是 更新到WP8必需知道的13個特性 系列的一個更新 希望這個系列可以給 Windows Phone 8開發者帶來一些開發上的便利。

同時歡迎大家在這裡和我溝通交流或者在新浪微網誌上 @王博_Nick

1. 鎖屏背景

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

Windows Phone 8 鎖屏背景與通知

代碼寫起來十分的簡單

首先還是在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字首。

Windows Phone 8 鎖屏背景與通知
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());
    }
}      
Windows Phone 8 鎖屏背景與通知

經過我的測試執行到這裡 LockScreenManager.RequestAccessAsync() 會彈出一個使用者提示 需要使用者确認。

// Setup lockscreen.
                if (!LockScreenManager.IsProvidedByCurrentApplication)
                {
                    await LockScreenManager.RequestAccessAsync();
                }      
Windows Phone 8 鎖屏背景與通知

當你在更新你的鎖屏背景時 尤其是從獨立存儲空間中讀取時 請盡量避免相同的檔案名經我測試相同檔案名有可能會造成系統預設緩存導緻圖檔更新延遲的情況發生。

MSDN也提供了一個替換名稱的方法

Windows Phone 8 鎖屏背景與通知
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.      
Windows Phone 8 鎖屏背景與通知

當然在運作程式之前我們不能用代碼幹預設定鎖屏背景在我們的程式中可以先預設一張圖檔作為鎖屏背景 但是這張圖檔的命名必須是 DefaultLockScreen.jpg且将這張圖檔放置在項目根目錄下.

同時在鎖屏設定頁面我們可以看到 open app的按鈕可以直接導航到我們的應用中去 這裡處理這個導航的方法和App to App 的方法類似 重載在App中處理InitializePhoneApplication方法 UriMapperBase即可 相信參考 windows phone 8 中的應用間通信

Windows Phone 8 鎖屏背景與通知
Windows Phone 8 鎖屏背景與通知
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.
    }
}      
Windows Phone 8 鎖屏背景與通知

當然在應用中也可以通過 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個應用程式的通知數量。

Windows Phone 8 鎖屏背景與通知

同樣在鎖屏設定中可以選擇設定顯示通知的應用及顯示即時狀态的應用。

Windows Phone 8 鎖屏背景與通知

下面介紹下應用如何實作這個通知

首先第一步還是要在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>      

其實就這麼簡單 隻要你的應用之前支援推送你 那麼經過以上的設定推送資訊就會顯示在鎖屏界面上了。

繼續閱讀