适用于:仅限于 Windows Phone 8。
在 Windows Phone 8,用户可以选择将应用用作锁定屏幕背景图像提供程序。
为使您的应用能够在手机中提供锁定屏幕背景图像,您需要在应用清单文件中声明应用的目的,并添加用于处理背景图像更改的代码。
本主题包含以下各节。
若要提供手机锁定屏幕背景图像,首先您需要更新应用清单文件,以将您的应用声明为锁定屏幕背景的提供程序。
-
在“解决方案资源管理器”中,展开“属性”,右击 WMAppManifest.xml,再单击“打开方式”,然后单击“带编码的源代码(文本编辑器)”。
-
在 <Extension> 元素中添加锁定屏幕背景 <Extension> 元素。如果<“扩展”>元素没有在文件中出现,则将下面的整个代码示例置于文件中。<Extensions> 元素必须放置在 <Tokens> 元素的下方。
XAML<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()); } }
唯一的图像名称
如果您从独立存储中更新锁定屏幕背景图像,每一次更新时,您都将需要提供唯一的文件名。可以实现这一点的简单方式是为文件名实现 A/B 切换逻辑。
以下代码演示 A/B 切换逻辑示例。
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.
如果用户在手机锁定屏幕设置屏幕中,将您的应用设置为默认锁定屏幕背景图像的提供程序,他们可能点按“打开应用”按钮,如下图所示。
点按“打开应用”按钮可以启动您的应用,信息包括在 QueryString 中。为提供最佳的用户体验,您应针对该信息的处理而设计应用;例如,当应用更新图像时,您可以将用户导向应用内的自定义锁定屏幕设置屏幕,或显示信息:锁定屏幕背景图像正在更新。
以下代码示例演示您如何可以检查 OnNavigatedTo(NavigationEventArgs) 方法中 QueryString 信息。
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. } }
为了您的应用的用户利益着想,请考虑添加指向手机的锁定屏幕设置屏幕的链接。这对于用户有用,因为您无法从应用内以编程方式关闭作为锁定屏幕背景图像的提供程序的应用。用户将需要访问手机的设置屏幕,自己进行更改。提供设置屏幕的链接使之变得直接简单。
以下代码示例显示您如何可以将按钮单击路由至手机的锁定屏幕设置屏幕。
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:")); }