In this article we are going to see how to programmatically control the Windows Phone Lock Screen from the application. In Windows Phone Lock screen can be customized with a background image from different application or from your own application or directly from Photo album as required. Its not all the application will have the capability of adding a Lock Screen functionality by itself, we need to enable the capability and add a custom permission to tell the application that we need a Lock Screen functionality to be enabled and available to the end user.
To enable a Lock Screen functionality first main thing that you need to think of the good image which should be showcased in the background as the Lock Screen. So Add any number of images to the Assets folder that are to be treated as Lock Screen Image. This is how the lock screen looks like when you allow the application to have lock screen enabled to apply the settings based on the selection.
Let us see the code on how to achieve enabling the Lock Screen information and also to assign the image by providing necessary privileges and permissions in the application manifest file in the project. Open Visual Studio and your application in which we need to enable the lock screen functionality. Followed by which add the images (Size is 480×800 pixel or 960×1708 pixel) to the Assets folder and make the resources as content to be on safer end you can cross check it. Now do the below steps
- In Solution Explorer, expand Properties, right-click WMAppManifest.xml, click view code option.
- Add the lock screen background <Extension> element. If the <Extensions> element doesn’t appear in the file, place the entire code example below into the file. The <Extensions> element must be placed below the <Tokens> element.
Code:
<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>
Now let us add the code in the main functionality to access and apply the Lock Screen functionality easily. To get this we need to add the namespace Windows.Phone.System.UserProfile to the code behind as we need this function to access the LockScreenManager class to play around with the functionalities. In the code first you need to get the permission from the end user to access and apply the lock screen asynchronously, upon user acceptance you can apply the lock screen image from the application easily. The below code does the same process on a click event.
Code:
private async void Button_Click(object sender, RoutedEventArgs e)
{
var isValid = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication;
if (!isValid)
{
var requestResponse = await LockScreenManager.RequestAccessAsync();
isValid = requestResponse == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted;
}
if (isValid)
{
var uri = new Uri(“ms-appx:///Images/BG.jpg”, UriKind.RelativeOrAbsolute);
Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);
MessageBox.Show(“Lock Screen Changed!!”);
}
}
In this code you can see the meta tag ms-appx which is used for resource bundling with the package (.xap) so once you are done now we are well set to proceed and you can have the lock screen changed as per the user acceptance.