Windows Phone 8 锁屏背景与通知

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

此文是升级到Windows Phone 8必需知道的13个特性系列的一个更新 但愿这个系列能够给 Windows Phone 8开发者带来一些开发上的便利。缓存

1. 锁屏背景网络

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

代码写起来十分的简单async

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

<Extensions>     测试

<Extension ExtensionName="LockScreen_Background"this

ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}"spa

TaskID="_default" /></Extensions>code

修改锁屏背景代码

这里我解释一下"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.Exceptionex)    

{

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>

若是还有困惑的话IT在线教育平台还有几个类似的例子。不过我想各位看官应该已经很清楚了。其实就这么简单,只要你的应用以前支持推送你 那么通过以上的设置推送信息就会显示在锁屏界面上了。

大功告成。

相关文章
相关标签/搜索