MOBILE

Themes on Windows Phone 7 Devices (part 1) - Applying a Theme

2/11/2011 9:31:27 AM
A Windows Phone 7 theme is a combination of a background and an accent color. Users can select from themes that ship with the phone, developers can access them in their code, and companies can alter them to match their own branding colors. Themes are set in the Settings => Themes portion of the Windows Phone 7 device or the emulator.

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.

  1. 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.

  2. 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.

  1. Go to the Theming project in Solution Explorer and open MainPage.xaml.cs (right-click MainPage.xaml and choose View Code).

  2. 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
Press F5 to run the application. The application screen should now display all of the controls you've added, including a white ellipse.
Other  
  •  Programming the Mobile Web : Mobile Widget Platforms
  •  Programming the Mobile Web : Geolocation and Maps - Showing a Map
  •  Mobile Application Security - BlackBerry Security - Permissions and User Controls (part 2)
  •  Mobile Application Security - BlackBerry Security - Permissions and User Controls (part 1) - RIM Controlled APIs
  •  Windows Phone 7 Development : Working with Controls and Themes - Introducing the Metro Design System
  •  Windows Phone 7 Development : WebBrowser Control - Saving Web Pages Locally
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 3)
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 2) - Google Gears
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 1) - W3C Geolocation API
  •  Programming the Mobile Web : Geolocation and Maps - Location Techniques
  •  iPhone Programming : Table-View-Based Applications - Connecting the Controller to the Model
  •  Programming the Mobile Web : Mobilizing WordPress and Other CMSs
  •  Programming the Mobile Web : Server-Side Browser Detection and Content Delivery - Content Adaptation
  •  Programming the Mobile Web : Multimedia and Streaming
  •  Mobile Application Security : BlackBerry Security - Development and Security Testing
  •  Mobile Application Security : BlackBerry Security - Introduction to Platform
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Dynamic Content
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Local HTML Content
  •  Windows Mobile Security - Networking
  •  Windows Mobile Security - Local Data Storage
  •  
    Video
    Top 10
    Windows Server 2003 : Domain Name System - Command-Line Utilities
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 2)
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 1)
    Brother MFC-J4510DW - An Innovative All-In-One A3 Printer
    Computer Planet I7 Extreme Gaming PC
    All We Need To Know About Green Computing (Part 4)
    All We Need To Know About Green Computing (Part 3)
    All We Need To Know About Green Computing (Part 2)
    All We Need To Know About Green Computing (Part 1)
    Master Black-White Copying
    Most View
    Collaborating Within an Exchange Server Environment Using Microsoft Office SharePoint Server 2007 : Exploring Basic MOSS Features
    Get A Faster, Safer PC (Part 3) - Make text easier to read, Disable a laptop touchpad
    Algorithms for Compiler Design: THE MACHINE MODEL
    iPhone, iPad touch and iPad : Realikety
    Storage, Screens And Sounds (Part 1)
    Audio Cleaning Lab MX - makes some sounds sound better
    SQL Server 2008 : Using Remote Stored Procedures
    Sony Xperia Go
    Web Security Testing : Manipulating Sessions - Analyzing Session Randomness with WebScarab
    Understanding the Basics of Collaboration in SharePoint 2010 : Microsoft Office Integration
    Buyer’s Guide - Keyboard and mice (Part 2) - Gigabyte Multimedia Ultra-slim Profile Keyboard GK-K6150, Microsoft Natural Ergonomic Keyboard 40000
    How To Store Your Files In The Ether
    Expert advice: Printer & Scanner (Part 2) - Samsung ML-2955DW
    SharePoint 2010 : PerformancePoint Services (part 2) - Using PerformancePoint
    SQL Server System and Database Administration : System Databases
    Is The Personal Blog Dead? (Part 2) - Going Mainstream
    Choosing The Right Gear For The Right Job
    Panasonic KX-MB1530CX Multi-Function Printer : A small workhorse for document printing
    Master Black-White Copying
    Windows 7 : Installing and Running Your Software (part 1)