Jul 18, 2011

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.

No comments: