歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Windows Phone 8 鎖屏背景與通知

Windows Phone 8 鎖屏背景與通知

日期:2017/3/1 9:53:51   编辑:Linux編程

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

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

升級到WP8必需知道的13個特性 系列文章目錄地址:http://www.linuxidc.com/Linux/2013-08/89003.htm

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.
Copyright © Linux教程網 All Rights Reserved