Updating Lock Screen from Windows Phone App

It is sometimes important for an application to change lock screen of the project. The Lock Screen can be set from an app only if the App supports an Extension to change lock screen. Let us take a look on how to change lock screen from an application.

First of all, Lock screen can be changed by an image from three locations :

1. Images located on app folder.
2. Images located on Internet.
3. Images coming from RSS.

The default lock screen size is 480px X 800px or 960px X 1708px.
Here are the steps to allow a program to change the lock screen.

1. In Solution Explorer right click on WMAppManifest.xml and open it with Text Editor.
2. Add lock screen extension to the application, without which the API to change the lock screen will not work.

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

Now to change the lock screen we need the following code.

private async void LockScreenChange(string filePathOfTheImage, bool isAppResource)
{
    if (!LockScreenManager.IsProvidedByCurrentApplication)
    {
        await LockScreenManager.RequestAccessAsync();
   
        var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
        var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);
 
        LockScreen.SetImageUri(uri);
 

        var currentImage = LockScreen.GetImageUri();
        MessageBox.Show("Lock screen changed. Click F12 or go to lock screen.");
    }
    else
    {
        MessageBox.Show("Background cant be updated as you clicked no!!");
    }
}

Here in the code above, we first try to call LockScreenManager.IsProvidedByCurrentApplication. If this API returns true, the application is then capable of changing lock screen. We then go ahead and RequestAccessAsync on the Change background.

Finally we use LockScreen.SetImageUri with an url pointing to an image with specified resolution to change the lock screen of the Windows Phone environment.
AppRequestMessage
The RequestAccessAsync will present you with a messagebox and upon clicking yes on the confirm, it will change the lock screen. The above screenshot is displayed when RequestAccessAsync is called form code.

The types of URL supported are :

ms-appx:/// for resources bundled within the .xap
ms-appdata:///local/ for local images within the root folder.

Hope this tip helps.

Thank you for reading

DON'T MISS OUT!
Subscribe To Newsletter For your Daily Development Tips and Tricks  

 Best Tips & Tricks on Microsoft Technology Platform 

Invalid email address
We promise not to spam you. You can unsubscribe at any time.
Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

3 Comments to “Updating Lock Screen from Windows Phone App”

Comments are closed.