Loading dictionary of resources runtime in WPF
This morning I had a little task, I’going to a Customer to install a simple WPF application in a touch screen. The problem is: I do not know how the customer want to format the form: font, color and so on. The problem is that I do not want to setup a procedural code to change these setting runtime, I only want to change the first time, then all goes on forever with that settings.
First of all I define a MainResources.xaml file with this content
|
|
As you can see this is an extract of the actual file, the important things are
all the formatting are defined with style
Look at line 3, since I use a custom IValueConverter I need to import the UILogic namespace specifying the assembly, we will see shortly why the standard declaration is not good.
Now all the windows imports this dictionary
|
|
This way to proceed has a big problem, the resources are compiled into the assembly, so if I want to change the style I need to change MainResources.xaml, recompile the application, and then redeploy in the customer computer….this sounds to me too work, especially if the customer want to do some variation, “font yellow, no better white, font 22 no better 24, lets try 28, no….it is better 22…..” can you imagine the frustration if I need to recompile for each little variation?
The solution is to load the dictionary runtime, here is why in the line 3 I need to specify the assembly *xmlns:UILogic=”clr-namespace:Proximo.Planner.Gui.Presentation.UILogic; assembly=Proximo.Planner.Gui”*because with dynamic loading the loader needs to know the assembly where my custom converter live in. Here is the code to load this file at RunTime
|
|
Wow…really simple isnt it? I simply use XamlReader.Load() method to load and compile XAML file at runtime, then I casted to ResourceDictionary, the root object of the XAML, then I simply replaced MergedDictionaries of the main Resources object.
Alk.
Tags:
Tags: WPF XamlReader Runtime Style