Manage reusable resources

May include but is not limited to: fonts, styles, data sources, images, resource dictionaries, resource-only DLLs

This topic goes hand in hand with the previous one (Styles), so I suggest you to read that one too. Resources are reusable elements in your application stored in some kind of repositories (in a DLL, in a resource collection, etc.). Let’s start with resource collections. These tools are found everywhere in the framework, mostly because the class FrameworkElement exposes a property called Resources, which is an instance of ResourceDictionary. You can imagine a resource dictionary: it holds a lot of resources, which are identified by a key (dictionary = a collection of keys + values). That’s why the all your resources should use a Key property (and in a dictionary, all of them should use a different one).

The most frequent place to store resources is the Window.Resources section. You can use static and dynamic resources. Let’s stick with static, When you use these, you must define the resource before the place you refer it. When you refer it, the framework starts a recursive search up the element tree to find the resource by the specified key. You should be aware of that the first resource will be used – that is the closest to the element that uses it.

I mentioned dynamic resources – the main difference is that static resources are grabbed once, when a request is made for them, and used from that time on – so theoretically they won’t reflect any change made to them (there are exceptions). Dynamic resources on the other hand pulled every time from the repository, so if you change a dynamic resource, the elements that use it reflect that change immediately.

So far we’ve placed our resources in places like Window.Resources. That can do it in most cases, but it’s worth knowing that there’s more. You can use resource dictionaries to store resources that you’d like to reuse later. A resource dictionary is a piece of XAML that’s single responsibility is to store resources. In order to use your resource dictionaries, you need to merge them in some place. A nice place to do so is Generic.xaml, but for example app.xaml can do the job for you:

      <ResourceDictionary Source=”Example1.xaml”/>
      <ResourceDictionary Source=”Example2.xaml”/>


So these were one type of resources. Of course, there are other types of resources, like images, fonts, sounds, and the rest. Obviously, you can’t store them in binary format in a XAML file, so let’s figure out how to get them.

These types of guys are known as assembly resources, because they are injected into the compiled assemblies as blobs. You add them to your project in the most convenient way: adding folders and placing, let’s say images in them. Use the build action Resource (not embedded resource, because it would be harder to get them there) in other words, don’t change the build action.

There are many ways to render an image – you can use very low level classes (compared to the average .NET Framework abstraction) like Application.GetResourceStream(Uri uri), or ResourceManager(string resourceName, Assembly assembly). What I’d recommend is the use of resource-aware classes. One of them is Image, and its Source property is perfectly happy with an Uri to the path of the image you’d like to render. Or in code, with the BitmapImage class (which takes the path as an Uri).

For last, let’s examine fonts. Embedding fonts is easy – just add them to your application, end set their build action to Resource. Then you can refer to them with the following strange syntax:

<Button FontFamily=”./#FontName”/>

“./” simply means the current folder, and # is used to identify fonts after it you should add the font’s real family name.


Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: