When we reference our resources, we can either use a StaticResource or a DynamicResource. If you remember from Chapter 5, Using the Right Controls for the Job, a StaticResource will look up the value of the resource just once, which is comparative to a compile-time lookup. A DynamicResource will repeatedly look up the value of the resource each time it is requested, whether it has changed or not, just like a runtime lookup.
For this reason, we should only ever use a DynamicResource if we really need to, as we can attain a much better performance by using the StaticResource class instead. If we find that we need to use a lot of DynamicResource references to access our resources, then we can refactor our code to data bind to properties in our StateManager class instead of the resources, in order to increase performance.
Another simple way to improve the performance of our resources is to reuse them. Instead of declaring them inline in the place that they are used in the XAML, we should declare them in a suitable resource section and reference them.
In this way, each resource is created just once and shared. To extend this idea further, we could define all of our shared resources in the application resources in the App.xaml file and share them between all of the application Views.
Imagine a situation where some brush resources were declared inline with the XAML within a DataTemplate element. Now imagine that this template is set as the ItemTemplate of an ItemsControl object and that the collection that is data bound to its ItemsSource property contains a thousand elements.
The application will, therefore, create a thousand brush objects with identical properties for each brush that is declared locally within the data template. Now compare this to another situation where we declare each required brush just once in a resource section and reference it from the template. It's clear to see the benefit of this method and the huge savings that can be made of the computer's resources.
Furthermore, this idea also affects the Resources sections of our Views, especially if we are displaying more than one of them at once. If we declare a View to define how each object in a collection should be rendered, then all of the resources that are declared in the View will be initialized once for each element in the collection. In this case, it is better to declare them at the application level.