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

Which, we will use in our demo.

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

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.