Setting the Lock Screen’s Background Image

,

The Windows Phone OS applies tight control over which, if any, of the lock screen’s fields an app can modify. The user must grant permission to an app to allow it to change the lock screen background image. The user can do this either by appointing your app as the lock screen background image provider in the phone’s settings (see Figure 13.10) or by accepting a confirmation dialog the first time your app attempts to change the lock screen’s background image.

Image

FIGURE 13.10 The phone’s lock screen settings page.

The SetBackgroundAsync method of the custom LockScreenService class demonstrates how to set the background image to an image located either in private storage or in the XAP package (see Listing 13.1).

Setting of the lock screen background image requires that your app be the sole lock screen background image provider for the phone. The value of the LockScreenManager class’s static IsProvidedByCurrentApplication property indicates whether the app is the provider. If it is not, the app must request to become the provider by calling RequestAccessAsync. Being the provider allows the app to set the lock screen background image using the LockScreen class’s SetImageUri method; passing in the URI of an image.

Prefixing the image URI with ms-appx:/// indicates that the image is located in the XAP, and prefixing it with ms-appdata:///Local/ indicates that the image is in local storage.

LISTING 13.1. LockScreenService.SetBackgroundAsync Method


public async Task<SetBackgroundResult> SetBackgroundAsync(
    string imagePath, ResourceLocation resourceLocation)
{
    bool isProvider = LockScreenManager.IsProvidedByCurrentApplication;
    if (!isProvider)
    {
            /* If the app has not been set as a provider in the phone's settings,
            * this call will prompt the user for permission.
            * Calling RequestAccessAsync from a background agent is not allowed. */
        LockScreenRequestResult requestResult
            = await LockScreenManager.RequestAccessAsync();

        isProvider = requestResult == LockScreenRequestResult.Granted;
    }

    if (isProvider)
    {
        /* 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. */
        string schema = resourceLocation == ResourceLocation.XapContent
            ? "ms-appx:///" : "ms-appdata:///Local/";

        Uri uri = new Uri(schema + imagePath, UriKind.Absolute);

        LockScreen.SetImageUri(uri);

        return SetBackgroundResult.Success;
    }

    return SetBackgroundResult.UserDenied;
}



Note

Calling LockScreenManager.RequestAccessAsync without the LockScreen_Background Extension element present in the app’s WMAppManifest.xml file raises an exception.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.73.102