Creating WP7 Custom Theme – Sample Theme Implementation
published on: 3/16/2011 | Views: N/A | Tags: Styling UI Blend
by WindowsPhoneGeek
I am starting a series of 3 articles: "Creating WP7 CustomTheme" in which I will explain everything you need to know about theming in Silverlight for Windows Phone 7. I will begin with a simple theme implementation based on color changes, next I will demonstrate how to implement a complex theme including some custom ControlTemplates and other custom logic. Finally I will share some best practices you need to consider when implementing custom theme.
- Creating WP7 Custom Theme - Basic Theme Implementation
- Creating WP7 Custom Theme - Complex Theme
- Creating WP7 Custom Theme - Best Practices
This is the first article so I am going to talk about how to implement a simple custom application theme in Silverlight for Windows Phone 7.
When you build a composite WP7 application with lots of controls it is essential that the whole UI will remain consistent in all themes. This is also an important part of the Windows Phone 7 Application Certification Requirements (Here's a direct link to the PDF file):
You have several options:
1.Create a custom application theme
Creating a custom application theme is a good solution when you want your application to look like in the same way in all theme i.e. to be theme independent. Another benefit is the fact that in this case you are sure that all your controls will look like consistent and in the same color variation regardless the phone theme.
NOTE: Adding a custom theme to your app override the default one!
2.Use the default theme resources: your app will look like different depending on the current phone theme
In this case if you use some of the default controls provided by the SDK like: Button, ListBox, Panorama, Pivot, etc you do not need to do anything and your app should remain consistent but it will look like in a different way depending on the current phone theme. By default Windows Phone uses a resource dictionary for theme resources, a keyed dictionary of objects that can be used both in XAML and in code. You can check this post for more info: WP7 Theme Resources Tips
Getting Started
Now lets begin creating a custom theme. The first thing to mention is that all Windows Phone 7 theme resources are places in the following folder:
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Design
Here is how the Design folder with all resources should look like:
Basically the most important files that you will need to modify are:
ThemeResource.xaml
This file contains all SolidColorBrushes, Colors and TextBlock resources.
Here you can find a list of all available resources: Theme Resources for Windows Phone
System.Windows.xaml
This is a more complex file. It contains all Styles/ControlTemplates of the default controls from the SDK like: TextBox, ListBox, Button, etc.
NOTE: Whatever changes you make to the control styles make sure that the final result fits in the Metro Design UI. It is probably a bad idea to change the appearance of the ToggleButton or ScrollViewer for example because your users will expect to see such controls in a WP7 Merto Like Style. However you are free to change the colors, sixes, etc
PortableUserInterface.Metro.CompositeFont
This file contains important information related to the Metro UI FontFamily, FontSize, etc. I would suggest that you should be careful with this file. It if possible try to not modify it!
Creating s Simple Custom Application Theme
The steps you have to follow in order to create a custom simple application theme are as follows:
1.) Create a sample Windows Phone 7 application and add a new CustomTheme into it.
2.) Copy the ThemeResource.xaml to your WP7 application and change the resources as you prefer. It is not so important whether you will create a separate folder like for example CustomTheme folder or you will just paste the file in the main root. However we would recommend to create a separate folder for your theme. In this example we will create CustomTheme folder and will paste the file there.
NOTE: The name of the file is not important. What is essential however is not to forget to copy all the content of this file! If you prefer you can change the name of the ThemeResource.xaml to something like CustomStyle.xaml or another.
NOTE: You can modify only some of the resources or all of them.
NOTE: Do not change the "x:Key" and "x:Name" values!
3.) Modify the resources: change some colors, brushes etc. to form a custom look. in our case we will change the following colors:
PhoneBackgroundColor - the base color of the PhoneBackgroundBrush
PhoneForegroundColor - the base color of the PhoneBackgroundBrush
PhoneAccentColor - the base color of the PhoneBackgroundBrush
NOTE: Here you can find a list of all available resources: Theme Resources for Windows Phone
We can use either VisualStudio or Expression Blend to make a custom theme.
- Using VisualStudiio
You can either do this on your own using VisualStudio by simply write for example:
<!-- Pink --> <Color x:Key="PhoneAccentColor">#FFF50C98</Color> <!-- Modified Color --> <Color x:Key="PhoneForegroundColor">#FF84F4F9</Color> <!-- Modified Color --> <Color x:Key="PhoneBackgroundColor">#FF39264E</Color>
- Using Expression Blend
Another option is to use ExpressionBlend which enables you to easily customize the appearance of every control using the Visual Designer. You can also modify all kind of resources and edit ControlTemplate ina n easy way. It is preferable to use Blend when creating themes in Windows Phone 7 because you can see the effect imediately in the designer. Here are some suggestions:
3.1. Go to VisualStudio right press the mouse and select "Open in Expression Blend":
3.2. You will see a popup with some security message, press YES:
3.3.Go to the Resources tab in ExpressionBlend and select ThemeResources.xaml (this is the name of your custom resource file):
3.4. Modify the desired colors by simply selecting the corresponding color picker:
The final generated code should be something like:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib"> ... <!-- Another resourced here!--> ... <!-- Pink --> <Color x:Key="PhoneAccentColor">#FFF50C98</Color> <!-- Modified Color --> <Color x:Key="PhoneForegroundColor">#FF84F4F9</Color> </ResourceDictionary> <!-- Modified Color --> <Color x:Key="PhoneBackgroundColor">#FF39264E</Color> </ResourceDictionary>
3.5. Save the changes and go back to VisualStudio. You will again see a message box that asks you to save the changes you have made in Blend. Click Yes to All:
4.) Go to App.xaml and add the following code:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CustomTheme/ThemeResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
NOTE: This code will override the default styles!
NOTE: Make sure that the Path to the file is correct and the Build Action as well. For more info about ResourceDictionary check this post: All about ResourceDictionary in WP7
5.) That`s it now you have successfully overridden styles.
Here is how our new theme looks like for some of the default SDK controls and the Silverlight for WP7 toolkit controls as well:
That was all about how to create a simple custom application theme in Silverlight for Windows Phone 7. You can find the full source code here:
I hope that the article was helpful. Stay tuned with the rest of the articles in this series.
You can also follow us on Twitter @winphonegeek
Comments
Pretty cool tutorial
posted by: Rahmed on 3/16/2011 5:40:17 PM
Pretty cool tutorial.
Just share some thoughts
posted by: Stevey on 3/16/2011 5:50:14 PM
Quite interesting! I was trying to do something similar in my app but did not manage to get it done. Your approach seems better. I will try it.
Something going wrong
posted by: Alex on 7/25/2011 11:19:44 PM
I've spent all day unsuccessfully trying to get this to work on my app.
My new colour scheme appears in the local resource list in Blend, and in both VS and Blend the design view preview is correct, but on the emulator and phone the new styles do not override the system theme.
I tried every combinition of Build Action/binding/etc I can think of! The sample project works fine and as far as I can tell I've copied it exactly, so I must be missing something stupid/small - is there anything I could have missed?
Note: I'm running the WP7.1 beta, but I also unsucessfully tried a new project targetting WP7.0.
RE:Something going wrong
posted by: winphonegeek on 8/1/2011 4:37:16 PM
We were unable to reproduce this issue. However we will continue to investigate it. For now as a workaround you could try to load your ThemeResources.xaml file in code behind like for example:
public App()
{
...
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
LoadDictionary();
}
private void LoadDictionary()
{
var dictionaries = Resources.MergedDictionaries;
dictionaries.Clear();
string source = String.Format("/WP7SampleProject23;component/TestStyles.xaml");
var themeStyles = new ResourceDictionary { Source = new Uri(source, UriKind.Relative) };
dictionaries.Add(themeStyles);
}
For reference take a look at this post:
Custom Mango Theme
posted by: winphonegeek on 8/17/2011 2:20:43 PM
We just posted two articles that explain how to create a Custom Application theme in Mango and how to port your existing WP7 theme to Windows Phone Mango as well:
Windows Phone Mango Custom application Theme Step by Step
How to port your WP7 custom application Theme to Windows Phone Mango
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
