Windows Phone Styles and Resources
For this blog entry I am just getting right to the point. I created a new project with just a button and immediately ran it. So I can change the theme in my emulator. I am setting the background to light, and the accent color to brown.
I want to find out how the application knows what color it should use. Does it even use another color?
Looking at XAML I can find this:
Inside the curly braces there are so called property bindings, or binding syntax. What it means is that this command is binding the property values to various elements on this page, like the font or the foreground. It is binding values to static resources that have been defined by Microsoft. A resource is any reusable value in Silverlight that has been set in one place and then is used whenever and wherever it is needed.
In this case the application gets the foreground color from the selection I made in the theme screen.
In my little application I could programmatically retrieve the user’s color selection from this point on by referencing that static resource named “PhoneForegroundBrush”.
But where is that “PhoneForegroundBrush” defined? It is not defined anywhere in my project. Well, there are embedded resource files that are already on a Windows Phone 7 device itself. They are referred to as “theme resources”. I can’t modify the themes or the style definitions within them, but as a developer I can choose which of the themes I want to use, or choose not to use any of them at all. Consequentially the themes are just simple XAML files, one for each combination of dark or light background along with each of the accent colors.
Here you can find a listing of all the various theme resources that have been created by Microsoft, and that I can pull from and use them within my application. Again, those are defined on the phone itself. I just make use of those resources by accessing them similarly to what I showed in the upper XAML screenshot of the binding syntax.
For this blog entry I am going to keep it simple. I am just using one of the resources that are defined on that website. The resource I am going to use is called PhoneAccentBrush. This is going to give me access to that brown accent color I selected earlier in the Windows Phone Emulator.
Suppose I would want to add an accent color to the button’s border. To accomplish this I have a couple of choices. The first is that I just could type the XAML directly into the code editor:
Looking at the visual portion of my designer I see that:
Now, that’s an interesting issue. I selected brown but I automatically get a blue binding around my button control. And also it still shows the dark background and not the selected light one. Why is that happening? Visual Studio uses some default settings in this case.
Running the application gives me the intended result though:
If anything, this just emphasizes the fact that Visual Studio and the Emulator work together but they’re two distinct applications that otherwise had no knowledge of each other.
Let’s move on to the property window. I am scrolling down to Foreground and right click on that little icon. A window is going to pop up where I select “Apply Resource…” Visual Studio then is going to show me a list of the various styles that I can choose from. I am choosing PhoneAccentBrush. Two things are happening. I can see that now the text on the button also shows up in blue:
The second thing that’s happening is that this property is added to my XAML code:
My button in the running application looks now like this:
For now I am resetting that Foreground property. That gives me the possibility to click that little arrow in order to select the wanted color:
This window has four tabs on the top. I can set a “Null Brush”, a “Solid Brush”, a “Gradient Brush” or a “Image Brush”. By default it is set to “Solid Brush” and the color white, everything set to 255, meaning that there is no alpha transparency at all. Anyway, there are lots of various possibilities to set the Foreground property. For now I want to use the “Gradient Brush”:
Remember, everything I do in the properties window will affect the XAML window somehow. My code for the Button Foreground property looks now like this:
It defines the StartPoint and then the GradientStop. When I run the application now, it will ignore the brown I chose before, but use the new defined Foreground property:
Assuming I would want to use the same LinearGradientBrush in other buttons that I want to add to my application, I can simply right click on that icon at the Foreground property again and select “Extract Value to Resource”. A window pops up where I can rename it and choose the destination where I want to save it:
If I want to use this GradientBrush color that I created only on this page, I would choose MainPage.xaml. If I want it to be available to the entire application I would choose app.xaml. Since I am only having that one page in my app I am going to choose MainPage.xaml.
Two things are happening again. First, the definition of the GradientBrush is gone, but I can see that the Foreground is now set to a StaticResource called Foreground1:
And when I scroll up to the PhoneApplicationPage I can see that there is now a Resource element as a child to the PhoneApplicationPage:
Let’s see how this works out. I am dragging a new button from the toolbox to my main form. In the properties window I scroll down to Foreground, right click that icon again and select “Apply resource”. I get a dialogue window where I can choose the defined “Foreground1” property:
Pretty cool, I think.
So, what if there are multiple properties for a given control, like my button control, that I want to apply to? For example, I not only want the text to be red, but also the border?
To do that, I am going to have to define a style, which is a collection of property setters.
First I am going to remove that LinearGradientBrush completely from my XAML code. Then I am going to define a style in XAML:
To use that style that I just created I simply need to remove the BorderBrush and the Foreground properties and then add that style to both of them:
Seems pretty simple.
To be continued…