Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 23 – Working with Themes in WP7

 

Introduction:

In this article we are going to see how to effectively use Themes in Windows Phone 7 development. As we all know a theme is a set of resources which are used to maintain the consistent look and feel for all the controls and resources used with in the Windows Phone 7 Application. Windows Phone 7 Themes plays a major role when developing applications where colors plays an important role.

Some of the advantages of using the Themes in Windows Phone 7 development is consistency across the default controls used with in the application without adjusting some of the common properties like colors, fonts, styles etc. We can override the theme anytime at run time and in the application level with out doing much changes. Themes in Windows Phone 7 is a combination of a background and an accent color. Background color is basically the background image color which we use in our application and accent color is the one that is applied to the controls used with in the application.     [more]

Background color with Windows Phone 7 have 2 options one is Light Color and other is the dark Color and as far as Assent color considered we have 10 options which we will see in detail in our upcoming articles. Windows Phone 7 Theme are selected using the Setting options, when the user selects the theme complete application system gets changed, but again when we change some other theme only the theme related color changes with in the application scope.

Theme resources are normally maintained in the TthemeResource.XAML file, the resources are maintained based on the background color and the accent color selected by the user or the developer. Theme resources files are available on the below location based on the OS which we are using.

  • 64-bit – DriveName\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Design
  • 32-bit – Drive\Program Fles\Microsoft SDKs\Windows Phone\v7.1\Design

Let us jump start to see the step by step process on how to use the themes in windows phone 7 development.

Steps:

Open Visual Studio 2010 and create a new Silverlight for Windows Phone 7 application with a valid project name as shown in the screen below.

clip_image002
Now let us start with adding some colorful controls to the MainPage.XAML file and see how the UI gets affected when changing the theme. Just copy the below XAML code or drag and drop the controls from the tool box and design the UI as shown in the screen below to get the same look and feel.


XAML Code:

01

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

02

<TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

03

<TextBlock x:Name="PageTitle" Text="Themes" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

04

</StackPanel>

05

 

06

<!--<span class="hiddenSpellError" pre="">ContentPanel</span> - place additional content here-->

07

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

08

<Rectangle Height="235" HorizontalAlignment="Left" Margin="38,33,0,0" Name="rectangle1"

09

Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="1" VerticalAlignment="Top"

10

Fill= "{StaticResource PhoneAccentBrush}" Width="163" />

11

 

12

<Rectangle Height="235" HorizontalAlignment="Left" Margin="259,33,0,0" Name="rectangle2"

13

Stroke="Aqua" StrokeThickness="1" VerticalAlignment="Top" Width="163"

14

Fill= "Aqua"/>

15

 

16

<Ellipse Height="253" HorizontalAlignment="Left" Margin="38,329,0,0" Name="ellipse1"

17

Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="1" VerticalAlignment="Top"

18

Fill= "{StaticResource PhoneAccentBrush}" Width="163" />

19

 

20

<Ellipse Height="253" HorizontalAlignment="Left" Margin="259,329,0,0" Name="ellipse2"

21

Stroke="Beige" StrokeThickness="1" VerticalAlignment="Top" Width="163"

22

Fill= "DarkGreen"/>

23

</Grid>

clip_image004

In the above XAML code, if we noticed we have added 2 Rectangles and 2 Eclipses to differentiate the usage of themes in real time. The first Rectangle is designed to get the application resources details to fill the shape border color and background color, where as for the 2nd rectangle we have directly specified the color without getting the resources from the application resource. Similarly we have done the same with the Eclipse as well that the first one uses the application resource and the 2nd directly assigned with the color.

Now to tell the Windows Phone run time environment to take care of the necessary changes the theme has to do for the selection add the below code to the MainPage.XAML.cs page constructor as shown in the screen below.


Code Behind:

1

// Background Colour

2

Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];

3

//Accent Colour

4

Color currentAccentColorHex = (Color)Application.Current.Resources["PhoneAccentColor"];

clip_image006

Now we are done with our code and we will build and execute the application by pressing F5 directly and we can see the application loads to the Windows Phone 7 Emulator as shown in the screen below.

clip_image008

We can see the output shows the different colour for the first rectangle and the first eclipse since we have assigned the Application resource where themes comes into the picture. Now let us change the theme and see how the theme affects the images. Click on the Start button from the emulator and go the application listing and select setting and then themes options. Now change the Background and the Accent colour and go back to the application to check the output we can see the colour again changes for the first rectangle and the first eclipse since they are controlled with the application resource as shown in the screens below.

clip_image010

Now on changing the themes we can see the complete colour shifting as shown in the screens below which is self explanatory.

clip_image012

Conclusion:

So in this article we have seen what are themes in Windows Phone 7 development and the difference between using the theme and not using theme when application resources are used in real time.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter