Themes on Windows Phone 7 Devices (part 1) - Applying a Theme
Themes can also be applied dynamically during the runtime of an application by overwriting or injecting the custom themes into Resources.MergedDictionaries found in Application.Current, as shown in the following code snippet:
ResourceDictionary res = new ResourceDictionary();
res.Source =
new Uri("/MyApplication;component/Assets/MyStyles.xaml", UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries.Add(res);
Currently, there are two possible background settings—Dark (default) and Light. There are ten accent colors to choose from, starting with a Microsoft-ish blue (the default) and ranging all the way to a decidedly 70s lime green.
NOTE
Microsoft recommends you use as little white color as possible (especially in backgrounds), since excessive use of white color may have a negative impact on battery life.
The combination of two background colors and ten accent colors provides the user with a total of twenty possible themes, delivering on the engagement and personalization promise of Metro design principles. Applications automatically adjust to the selected theme and ensure that all UI elements appear consistently across the platform. A quick walkthrough demonstrates theme-awareness of Windows Phone 7 controls and UI elements.
1. Applying a Theme
In this walkthrough, you will add a set of Windows Phone 7 controls to an application, creating some of them with XAML and some through managed code. You will change the theme in the emulator and observe the effect this change has on those controls. Follow these steps to get a better understanding of theming support in Windows Phone 7.
1.1. Creating a User Interface
First, you will add a set of standard controls to a Windows Phone 7 application.
Launch Visual Studio 2010 Express and select the Windows Phone Application template. Change the Project Name to "Theming," select OK, and Visual Studio will set up a new project.
Open MainPage.xaml in design mode and add a text box, textblock, check box, button, and a black rectangular shape to the page. Your end goal is a simple interface that resembles the one in Figure 9-2. Here's the XAML:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="THEMES AND COLORS" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="THEMES" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox Height="72" HorizontalAlignment="Left" Margin="-4,6,0,0" Name="textBox1"
Text="TextBox" VerticalAlignment="Top" Width="454" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="11,80,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="329" />
<CheckBox Content="CheckBox" Height="72" HorizontalAlignment="Left" Margin="12,116,0,0" Name="checkBox1" VerticalAlignment="Top" />
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="9,194,0,0" Name="button1" VerticalAlignment="Top" Width="160" />
<Rectangle Height="110" HorizontalAlignment="Left" Margin="249,137,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="156" />
</Grid>
</Grid>
1.2. Adding Code to Draw an Elliptical Shape
In addition to using the powerful Visual Designer to add controls to Windows Phone 7 pages, as you just did, you can add controls programmatically. The steps here show you how to do that.
Go to the Theming project in Solution Explorer and open MainPage.xaml.cs (right-click MainPage.xaml and choose View Code).
To add a white ellipse to the page, paste the following code inside the MainPage constructor:
Ellipse e = new Ellipse();
e.Width = 100.0;
e.Height = 120.0;
e.StrokeThickness = 2.0;
e.HorizontalAlignment = HorizontalAlignment.Left;
e.VerticalAlignment = VerticalAlignment.Top;
Color backgroundColor = Color.FromArgb(255, 255, 255, 255);
e.Fill = new SolidColorBrush(backgroundColor);
e.Margin = new Thickness(10, 300, 10, 10);
ContentPanel.Children.Add(e);
Figure 1. Theming application user interface layout