Saturday, January 22, 2011

Windows Phone 7: Detecting the WP7 Theme

The Windows Phone 7 comes with two background color modes dark or light. The easiest way to identify the Theme that the user has choosen in the device can be found via the Application Resources .

In the Development Environment , you can find the file ThemeResources.xaml in the path
C:\Program Files\Microsoft SDKs\Windows Phone\v7.0\Design

The easiest way to find if the Dark or Light Background is visible is again by using the Resources string PhoneLightThemeVisibility or PhoneDarkThemeVisibility which will give you information if visible or collapsed . You can encapsulate this logic in your own class and use it throughout your application.

   1:   public class Settings
   2:      {
   3:          public static Settings Instance = new Settings();
   4:   
   5:          private Settings()
   6:          {
   7:          }
   8:   
   9:          public Theme CurrentTheme 
  10:          {
  11:              get { return (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Visible? Theme.Light: Theme.Dark ; } 
  12:          }
  13:      }
  14:   
  15:      public enum Theme
  16:      { 
  17:          Light,
  18:          Dark
  19:      }

No comments: