adamnathan.netSubscribe to this blogMetro Web BrowserTODO ListAlarm ClockTip CalculatorSound RecorderGroceriesPick a CardNotepadStopwatchMetronomePinned SearchesFake CallMood RingCocktailsTODO ListAlarm ClockTip CalculatorSound RecorderGroceriesPick a CardNotepadStopwatchMetronomePinned SearchesFake CallMood RingCocktails
 Tuesday, November 02, 2010

I thought I'd post some tips from my Windows Phone 7 book, and what better way to start than by answering some questions my new boss posted on Twitter!

Most of the time, you should try to not care which theme is in use by sticking with the various brushes/colors defined as application resources, such as PhoneForegroundBrush and PhoneBackgroundBrush. Sometimes you can't avoid it, though, and you want to write code such as this:

if (/* dark theme */)
    // Do dark theme stuff
else
    // Do light theme stuff

To accomplish this, you can leverage one of several pre-defined application resources:

  • PhoneDarkThemeVisibility, of type Visibility (Visible or Collapsed)
  • PhoneLightThemeVisibility, of type Visibility (Visible or Collapsed)
  • PhoneDarkThemeOpacity, of type double (0 or 1)
  • PhoneLightThemeOpacity, of type double (0 or 1)

Therefore, here are four different yet equivalent ways to express the preceding pseudo-code:

if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
    // Do dark theme stuff
else
    // Do light theme stuff

 

if ((Visibility)Application.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Collapsed)
    // Do dark theme stuff
else
    // Do light theme stuff

 

if ((double)Application.Current.Resources["PhoneDarkThemeOpacity"] == 1)
    // Do dark theme stuff
else
    // Do light theme stuff

 

if ((double)Application.Current.Resources["PhoneLightThemeOpacity"] == 0)
    // Do dark theme stuff
else
    // Do light theme stuff

 

I used this technique in Tip Calculator to define my own theme-specific brushes for the bottom buttons that match the built-in Calculator app's buttons:

 

This was necessary because, alas, the necessary brushes are not already defined as application resources.

Tuesday, November 02, 2010 4:23:00 AM (Pacific Standard Time, UTC-08:00)  #    Comments [0] - Trackback    
Comments are closed.
<November 2010>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
Adam's Books
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© 2014 Adam Nathan