Windows Phone 8 锁屏背景与通知

来源:互联网 发布:会计记账软件 编辑:程序博客网 时间:2024/06/05 09:57


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>

 

其实就这么简单 只要你的应用之前支持推送你 那么经过以上的设置推送信息就会显示在锁屏界面上了。

0 0