Windows Phone 8 在锁屏背景图片是支持应用自定义的,并且在屏幕下方还支持应用通知提醒,这是一个十分吸引眼球的新功能 虽说目前已经看到很多应用已经做个了个特性今天我还是在这个里为大家相信说明一下 为后面想做这个功能的同学先铺铺路。

此文是 升级到WP8必需知道的13个特性 系列的一个更新 希望这个系列可以给 Windows Phone 8开发者带来一些开发上的便利。

同时欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick

1. 锁屏背景

正如我说windows phone 8 是支持锁屏背景的替换的 下图是摘自MSDN的一张原图很好理解

代码写起来十分的简单

首先还是在WMAppManifest文件中声明下 这段XML要紧跟在<Tokens>节点后面



1. <Extensions> 
2. "LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default"
3. </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前缀。


1. private async void LockHelper(string filePathOfTheImage, bool isAppResource) 
2. { 
3.     try 
4.     { 
5.         var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication; 
6.         if (!isProvider) 
7.         { 
8. not the provider, this call will prompt the user for
9. from a background agent is not
10.             var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync(); 
11.  
12. Only do further work
13.             isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted; 
14.         } 
15.  
16.         if (isProvider) 
17.         { 
18. At this stage, the app is
19.  
20. schema. 
21. to the root of the local
22. to the Local app install folder, to reference resources bundled in
23. schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/"; 
24. schema + filePathOfTheImage, UriKind.Absolute); 
25.  
26. Set
27.             Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri); 
28.  
29. of
30.             var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri(); 
31. "The new lock screen background p_w_picpath is set to {0}", currentImage.ToString()); 
32.         } 
33. else
34.         { 
35. "You said no, so I can't update your background."); 
36.         } 
37.     } 
38.     catch (System.Exception ex) 
39.     { 
40.         System.Diagnostics.Debug.WriteLine(ex.ToString()); 
41.     } 
42. }

经过我的测试执行到这里 LockScreenManager.RequestAccessAsync() 会弹出一个用户提示 需要用户确认。


1. // Setup lockscreen. 
2. if (!LockScreenManager.IsProvidedByCurrentApplication) 
3. { 
4.     await LockScreenManager.RequestAccessAsync(); 
5. }

当你在更新你的锁屏背景时 尤其是从独立存储空间中读取时 请尽量避免相同的文件名经我测试相同文件名有可能会造成系统默认缓存导致图片更新延迟的情况发生。

MSDN也提供了一个替换名称的方法



1. string fileName; 
2. var currentImage = LockScreen.GetImageUri(); 
3.  
4. if (currentImage.ToString().EndsWith("_A.jpg")) 
5. { 
6. "LiveLockBackground_B.jpg"; 
7. } 
8. else
9. { 
10. "LiveLockBackground_A.jpg"; 
11. } 
12.  
13. var lockImage = string.Format("{0}", fileName); 
14.  
15. // At this point in the code, write the p_w_picpath to

当然在运行程序之前我们不能用代码干预设置锁屏背景在我们的程序中可以先预设一张图片作为锁屏背景 但是这张图片的命名必须是 DefaultLockScreen.jpg且将这张图片放置在项目根目录下.

同时在锁屏设置页面我们可以看到 open app的按钮可以直接导航到我们的应用中去 这里处理这个导航的方法和App to App 的方法类似 重载在App中处理InitializePhoneApplication方法 UriMapperBase即可 相信参考 windows phone 8 中的应用间通信



1. protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
2. { 
3.     base.OnNavigatedTo(e); 
4.  
5. "WallpaperSettings"; 
6. "0"; 
7.  
8. out
9.  
10.     if (lockscreenValueExists) 
11.     { 
12. user to
13. or indicate that the lock screen background p_w_picpath is
14.     } 
15. }

当然在应用中也可以通过 LaunchUriAsync 方法打开设置页面 相信参考:http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662937(v=vs.105).aspx



1. private async void btnGoToLockSettings_Click(object sender, RoutedEventArgs e) 
2. { 
3. for
4. "ms-settings-lock:")); 
5. }

2. 锁屏通知

windows phone 7 中已经包含了推送通知,在windows phone 8中开发者可以将其融入到锁屏界面中来

下图还是一张选自MSDN的截图很清晰的说明通知的情况,左侧大字是显示的应用的详细状态,下面一行可以显示5个应用程序的通知数量。

同样在锁屏设置中可以选择设置显示通知的应用及显示即时状态的应用。

下面介绍下应用如何实现这个通知

首先第一步还是要在WMAppManifest中声明我们的应用是一个支持锁屏通知的应用同时指定通知的图标来源。

图标必须是一个白色背景透明 38 x 38 像素的PNG 图片。

在Token节点中

1. <DeviceLockImageURI IsRelative="true" IsResource="false">Assets\LockImage.png</DeviceLockImageURI>

其次在声明支持通知 前者是声明支持显示应用显示即时状态 后者是声明显示应用显示详细状态。



1. <Extensions> 
2. "LockScreen_Notification_IconCount" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default"
3. "LockScreen_Notification_TextField" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default"
4. </Extensions>

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

同时欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick


转载于:https://blog.51cto.com/nick287/1162192