×
Namespaces

Variants
Actions
Revision as of 06:37, 3 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Windows Phone8中的锁屏背景和通知

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon UI.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata

代码示例
兼容于
文章
Xinx Gong 在 20 Dec 2012 创建
最后由 hamishwillee 在 03 Jul 2013 编辑

Contents

锁屏背景

在Windows Phone 8中,用户可以选择将应用用作锁屏背景图片的提供应用。

获取当前手机的分辨率

由于在手机中支持三种分辨率,为了能提供和当前用户手机分辨率适合的图片,我们需要先获取当前手机的分辨率。

private bool IsWvga
{
get { return App.Current.Host.Content.ScaleFactor == 100; }
}
 
private bool IsWxga
{
get { return App.Current.Host.Content.ScaleFactor == 160; }
}
 
private bool Is720p
{
get { return App.Current.Host.Content.ScaleFactor == 150; }
}

更新WMAppManifest.xml文件

用文本编辑器的方式打开应用的WMAppManifest.xml文件。在<Tokens>元素下面添加<Extensions>元素,并添加对应的<Extension>,代码如下:

<Extensions>
<Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" />
</Extensions>

更改锁定屏幕图片

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());
}
}

获取当前锁屏背景图片的Uri

var currentImage = LockScreen.GetImageUri();

设置默认的锁屏背景图片

在您的应用可以设置锁屏背景图片之前,用户可以在手机锁屏设置页面中将您的应用设置为锁屏背景图片的提供程序。此时,您应在您的工程的根目录上添加一张默认锁屏背景图片。将该图片命名为 DefaultLockScreen.jpg。

从您的应用跳转到系统锁屏背景设置页面

await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-lock:"));

从系统锁屏背景设置页面打开您的应用

从系统锁屏背景设置页面打开应用时,应用将获取到的NavigationUri为:

/MainPage.xaml?WallpaperSettings=1

您可以创建新的UriMapper来处理。UriMapping的用法

锁屏通知

创建锁屏应用识别图标

需要一张30*30的png图标。图标必须仅包含白像素和一定级别的透明度。 用文本编辑器的方式打开应用的WMAppManifest.xml文件。找到<Tokens>元素下的< DeviceLockImageURI>,修改为如下代码:

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

更新WMAppManifest.xml文件

用文本编辑器的方式打开应用的WMAppManifest.xml文件。在<Tokens>元素下面添加<Extensions>元素,并添加对应的<Extension>,代码如下:

<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>

LockScreen_Notification_IconCount对应的是数量通知,LockScreen_Notification_TextField对应的是计划文本通知,如果仅需要支持其中某一个,只需要添加对应的< Extension>就行。

源码下载

File:Wp8 lockscreen sample.zip

This page was last modified on 3 July 2013, at 06:37.
202 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×