Disabling Windows Phone 7 lockscreen aka Idle detection

Ready for another really useful code snippet? 😀

This time its few lines about the prevention of the lockscreen appearance. Everyone knows about it, but not everyone is really sure how to use it and what are the consequences of its use.

First things first, lets learn how to use it.


Everything we need here is only one property, the UserIdleDetectionMode of the current phone application service. It can be set to either enabled or disabled, and is always recommended to store its current state before you change it, so that you can set it back to whatever value it had before you started messing with it. This is best done  by using a private variable in your App.xaml.cs. Launching and activating events should be used to store the original setting prior changing it to the mode you want, and deactivating and closing events for setting the original setting back.

 

private void Application_Launching(object sender, LaunchingEventArgs e)
{
    _originalMode =PhoneApplicationService.Current.UserIdleDetectionMode;
    PhoneApplicationService.Current.UserIdleDetectionMode= IdleDetectionMode.Disabled;
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    _originalMode = PhoneApplicationService.Current.UserIdleDetectionMode;
   PhoneApplicationService.Current.UserIdleDetectionMode= IdleDetectionMode.Disabled; 
}

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
   PhoneApplicationService.Current.UserIdleDetectionMode = _originalMode ;
}

 private void Application_Closing(object sender, ClosingEventArgs e)
{
    Microsoft.Phone.Shell.PhoneApplicationService.Current.UserIdleDetectionMode = _originalMode ;
}

And that’s it.. I’ve set the mode to disabled in the code snippet but you could set it to enabled it it suits your needs..

In the end, it is important to be very careful with switching this property of, as it could drain your battery in matter of hours as your app will never be deactivated by a lockscreen.. please take some time to examine the important notes on this page before you start playing with it.

Setting image source programmatically depending on the current phone theme

Every Windows Phone 7 application needs to support both dark and light themes, so it is sometimes needed to change the image displayed depending on the current phone theme. At the moment, there is only one way to find out the theme that is currently being used and it is achieved by examining the PhoneLightThemeVisibility resource. Its not the most clever way to go, but its all we have for now.

Visibility v = (Visibility)Resources[“PhoneLightThemeVisibility”];
String url = v==Visibility.Visible?
“/MYAPP;component/Icons/Light/checkWithCircle.png” : “/MYAPP;component/Icons/Dark/checkWithCircle.png”;

Uri imgURI = new Uri(url, UriKind.Relative);
UniversalImage.Source = new BitmapImage(imgURI);

Hope it helps!

P.S. if you are trying to access Resources from something other that UIElement (e.g. from the ViewModel class), you could use Application.Current.Resources to get the PhoneLightThemeVisibility resource.

Kao malo sam promenio layout u temu…

…ali i nisam bas zadovoljan… ništa dok sam ne zbudžim pozadinu u PS i malo čačnem HTML lejaouta…

Radi se, kao što i sami vidite. Puna mi je glava stvari koje bi valjalo izblogovati ali nikako da nadjem dovoljno vremena da to i uradim…

i aj kad sam tu da skrenem pažnju na nešto. Ukoliko imate developer WP7 phone, možete apdejtovati sistem na Mango!