Floating FB popout byF5debug

How to control the Windows Phone Lock Screen Programmatically

 

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.

image

 

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 480x800 pixel or 960x1708 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>

 

image

 

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!!");
            }
        }


image

 

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.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter