Windows Phone 8 在锁屏背景图片是支持应用自定义的,并且在屏幕下方还支持应用通知提醒,这是一个十分吸引眼球的新功能 虽说目前已经看到很多应用已经做个了个特性今天我还是在这个里为大家相信说明一下 为后面想做这个功能的同学先铺铺路。此文是 升级到WP8必需知道的13个特性 系列的一个更新 希望这个系列可以给 Windows Phone 8开发者带来一些开发上的便利。升级到WP8必需知道的13个特性 系列文章目录地址:http://www.linuxidc.com/Linux/2013-08/89003.htm1. 锁屏背景正如我说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.