Change Your Lock Screen in Windows Phone

Introduction

In this article, we are targeting Lock Screen manipulation. In earlier versions, you can change the Lock Screen's image programmatically. But, in Windows Phone 8 SDK you can do it easily with a few lines of code.

Lock Screen

Agenda
  • First, we will add some essential Manifest (meta-data) data in WMAppMainifest.xml
  • Add a dummy image in your Solution Explorer.

Which, we will use in our demo.

  • Now, try to code.

Procedure

Step 1

Windows Phone doesn't allow all the applications to play with Lock Screen settings except those apps with permission to do so.

Every Windows Phone app has a manifest file (in other words WMAappMainefest.xml) that contains the list of permissions in XML format.

So, we can do this by adding a few lines to our manifest file as in the following:

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

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

</extensions>

If you can't get these, then it is genuinely fine because these lines are permissions and internally handled by Windows Phone itself.

Note: Put these lines just below the end tag token (in other words </token>).

Step 2

Design a simple page that has a button, in that event we change the Lock Screen's image.

change the lock screen image

And add this namespace to your project
 

using Windows.Phone.System.UserProfile;

 

private async void LockScreenChange(string pathOfImage,bool isAppResource)

{

        try

        {

            var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication;

            if (!isProvider)

            {

                // Ask for Permission

                var op=await LockScreenManager.RequestAccessAsync();

                //Now, we will Grant The Permission

                isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted;

                // MessageBox.Show("Sent");

            }

            //If, You Got the Permission then, Change The Lock Screen

            if (isProvider)

            {

                //var schema=isAppResource?"ms-appx:///":"ms-appdata:///Local/";

                var uri = new Uri("ms-appx:///Images/1.jpg", UriKind.RelativeOrAbsolute);

                //Set The Lock ScreenImage

                Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);

                //get The URI of Lock screen Image

                MessageBox.Show("Successfully Done");

           }

          else

          {

                MessageBox.Show("ERROR");

          }

    }

    catch (Exception e)

    {

        MessageBox.Show(">> Exception : " + e.Message);

    }

} 

And, we call this method when we trigger the button event,

private void Button_Click(object sender, RoutedEventArgs e)
{
    LockScreenChange("Images/1.jpg",true);
}

Explanation

  • In the LockScreen() method we have paased two arguments, one is the path of the Image and the second is the kind of resource (AppResource or Isolated Resource).
  • In the Try {} block, we try to detect the wheather the App has the Lock Screen permission or not. If yes, then it will go for the Lock Screen code, else it will ask for the permission.
  • If yes, you have the permission to change the Lock Screen, then get the URI for the image resource. And it will do with a specified way what Windows Phone understands Either, ms-appx or ms-appdata will be joined before the URI of the image.
  • Finally, we call the SetImage() method to set.

Output

After successful debug, you will get something like this:

Output

And, when you click on the Change button then:
 
Change button

After tapping on YES, you have your own Lock Screen.

Conclusion

You have seen many apps on the Store that does the same thing. And, one of them is mine.

windowsphone

Regarding this demo, go with the solution file if you have any issues.

Up Next
    Ebook Download
    View all
    Learn
    View all