Resources in Expression Blend
We will be using Microsoft Expression Blend 4 for this tutorial.
Resources are integral part of WPF and Silverlight. A resource in nothing but a name and value pair. Say for ex – If I talk to a 2nd grade student and say my phone’s color is #FF000000, he would not understand what’s that that but when I say my phone’s color is black, he might or might not know what I am talking about.
This is the concept used by Microsoft behind using resources. This enables us to think in terms of names and labels as opposed to talking about the values. Let see this working in Expression Blend.
- Create a new project in Expression Blend and name in Resources.
- Create a rectangle on to the design surface.
- Go to the properties tab and select the color green. Now when you work for any clients or customer designing applications for them they would have specification of color coding standards they want you to follow. Resources follow the same trend so that you can name your resource and use it at multiple places by just referring its name. We will see how that happens a little later.
Click on the PropertyMarker as shown in the Image and select ConvertToNewResource. Every property in Blend has a property marker that means we can convert all the property in blend into a resource.
- Just name the property as MyGreen and click ok.
- Now as would notice a couple of things have changed. Instead of the ColorPicker now we see Color resources and the also the list of resources available. Also as you can see the MyGreen is highlighted It is the Color that I am using currently.
- Now just create a Ellipse and by default you would see that the color selected is the MyGreen color as Blend figures out that MyGreen is the color I am working and probably that’s the color I need.
- Now Select the Fill as Gradient Brush
- Now go to the Property Marker for the Gradient Brush and Go To local resource and select MyGreen. As you see are using MyGreen as both a SolidColor and a Gradient Color. The Color is the same but we are using it at different places.
- Now go to the resources tab at the top and expand that user control and you would see that the MyGreen resource is available in the UserControl Scope.
- Now if we change the color here we are able to change the color at all the controls using this resource. So using resource you don’t have to go to each and every control and change the color.
- Now go back to selection tool and select the Ellipse. Now go to the Fill property marker of the Ellipse and select ConvertToNewResource.
- Name it as BlackToMyGreen.
- Now we see a Local Brush resource is available. You should know that a color is just is a 24bit value that probably represents some transparent color in the spectrum but a brush can be a single color or a set color or something else.
- Now let’s go back to the Resources tab and expand the UserControl again and change the MyGreen to some other value. Now you would see that not only MyGreen has changed but also the BlackToMyGreen has also changed, so this implies that resources can use other resources as well.
Scope of Resources in Expression Blend
Now let’s talk about Scope of resources. Scope of a resource simply means where are we going to look when we are getting a concept. Let’s talk about a 2nd grader to whom we say my phone is black. Now he might know what is black, if that’s the case he is going immediately know what color you are talking about. Let’s say he doesn’t know what is black so he’s going ask his parents. Maybe his parents are able to answer it or they then go to their parents and ask what does black mean? This is exactly what scope means. You can define a concept at any level – object level, on a parent, on a document, in an application or a resource dictionary. Now when we say MyGreen then Silverlight or WPF will be able to figure out what is MyGreen.
Let’s see this with the help of an example:
- Delete the rectangle and grid in the application we are working on and draw 2 identical Grids as shown below:
- Now select the left grid and go to its properties. And select the background as Green.
- You can click on the shortcut key and convert a color to resource.
- And we will name it Grass and we will define it in the grid instead of the usercontrol.
- Now give a background to the other grid and create a new color resource in that grid named as Grass.
- Clear the brush on both the grids using the shortcut below:
- Create a rectangle in one of the two grids. And using the color resources make the Fill of the rectangle as Grass.
- Now using the selection tool drop the rectangle from one grid to another by keeping the alt key pressed.
- What you see now is that the rectangle is using the local Resource Grass. Whenever an element is trying to figure out the meaning of the defined resources, it first looks into its own defined resources, then its parent resources and if it still is not able to find it will look further up till the root of the UserControl. And if still it is not able to find it there it will look for it in the application resources. But if a resource is defined at multiple levels then the closest one is adopted by the element. i.e. the resource is defined at a parent level as well as the application level then one the parent returns the meaning of the resource then the element stops looking and uses the same.
- Let’s take another rectangle and drop it on the panel and this rectangle also uses the Fill as Grass but is not able to find it. That’s because the Rectangle does not know what Grass is, it asks LayoutRoot but LayoutRoot also does not know what Grass is, then it asks the UserControl which still does not know what grass is. So the unresolved resource gives us a blank fill.
- Let’s go ahead and give it a SolidColorBrush and set the alpha value of it to 100 as whenever we get an unresolved resource blend gives it a transparent white brush.
- Now convert this to resource and select it to save at the application level.
- Let’s go the resources tab and we see that Grass is defined as different color for different levels.
Resource Dictionary in Expression Blend
Let’s again talk about our 2nd grader who went to his parents to ask what is black, who in turn ask their parent as to what is black. Say their parent also does not know what is black so what they would do is grab one of the reference guides or resource dictionary and look for black color, if that resource dictionary does not have an answer for blue then they will check in other available resource dictionaries. In this case a Resource Dictionary is a single xaml file that contains key value pairs in it.
Let’s have a look at this with the help of an example:
- Create a new solution in Blend named ResourceDictionaries and create a rectangle in it.
- Convert that into a resource. But we will define it in a new ResourceDictionary.
- As Soon as we do that a new xaml is added to the project. The reason for giving description of MyGreen here instead of the application level is the same as printing all the information in the book in the 2nd grader example. We can very easily transfer this dictionary to a different application.
- Let’s just create a couple of more dictionaries. Right click the project and select Add new item and select Resource Dictionary and click ok.
- Now we go the rectangle and change the fill color and save it into the resource dictionary 2. Change the color again and save it to the resource dictionary 3.
- Now Delete the rectangle and just drag and drop a new one. What you will see is that this rectangle’s fill is yellow i.e. the color from the last resource dictionary. This happens because Blend creates a merged dictionary. So when a resource is found in a resource dictionary the element will look for any other occurrences of the same resource in all the resource dictionaries and will override the previous values with the new one. Below is how the resource dictionaries look like.
- We can add or remove reference to a resource dictionary using the interface below.
- And when we do the colors will follow a fallback mechanism.
I would like to state here that as of now i.e. Silverlight 4 all the resources are static resource and will take the value at the time instantiation. Where as in WPF we do have Dynamic Resources and at runtime everything will update.
Please find the source sode for the soultions at location below:
http://i.minus.com/1337680217/iM-3YUH7dLJB6ELGpiplpQ/dbuofcNvmgf7Uz.zip
http://i.minus.com/1337680215/Uq65IIajg7rV01b2c715dQ/dbvtJW3hnSasrW.zip
Any comments, feedbacks or question are most welcome.