DCSIMG
Managing WPF resources contained in external assemblies - Ariel's Remote Data Center

Managing WPF resources contained in external assemblies

Posted Sep 15 2009, 02:10 PM by Ariel Ben Horesh  

Here is a small how-to solution to a very common question.

Suppose I have separated my client application to multiple projects, and each project contains his own WPF resource dictionary, how do I use it?

Bottom line, you need to merge your resource dictionary to the application main resource, let’s see how to do that.

private static void RegisterResources()
    {
        ResourceDictionary dictionary = new ResourceDictionary();
        dictionary.Source = new Uri(
            "pack://application:,,,/[ASSEMBLY NAME];Component/[SUBFOLDER IF ANY]/[DICTIONARY NAME].xaml");
        Application.Current.Resources.MergedDictionaries.Add(dictionary);
    }

Note: the strange Uri syntax, every [] is a place holder for you to put in your specific information.

I usually use this method somewhere in my module entry point. (If using prism, the class which implement IModule is a good candidate).

Ariel

תגים:, ,


Comments

pavely said:

Could you do the same in XAML?

# September 15, 2009 9:37 PM

Ariel Ben Horesh said:

@pavely, there is a xaml syntax for it. but in this scenario, nobody is going to use thiis xaml. unless you do something ugly like forcing a xaml file to be rendered.

this is mucj cleaner.

if you have a simple one project application that all the UI is already there, it is much simple to do it by xaml.

For example:

   <Application.Resources>

       <ResourceDictionary>

           <ResourceDictionary.MergedDictionaries>

               <ResourceDictionary Source="Resources\DefaultThemeControls.xaml" />

           </ResourceDictionary.MergedDictionaries>

       </ResourceDictionary>

   </Application.Resources>

# September 16, 2009 3:10 AM

Twitter Trackbacks for Managing WPF resources contained in external assemblies - Ariel's Remote Data Center [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Managing WPF resources contained in external assemblies - Ariel's Remote Data Center         [microsoft.co.il]        on Topsy.com

# September 16, 2009 3:12 AM

Josh said:

Hi buddy,

Question: you are using CL / Prism with dynamically loaded modules (each of which has its own set of resource dictionaries ) using a directory catalog - you cannot reference the assembly in the pack Uri because the Shell WPF app doesnt  have a project reference to the Dll - what do you recommend doing in such a situation?

# September 17, 2009 12:42 PM

Ariel Ben Horesh said:

Hi Josh,

You put this registration code inside your Module Entry point (assuming your resources file are part of the module).

It doesn't matter ( I think) how the module is getting loaded, either through directory catalog or configuration. The Shell in either case doesn't have a reference to the modules.

In other words, my solution should work for you.

if You wish I can upload a quick sample.

# September 17, 2009 4:26 PM

Ariel's Remote Data Center said:

A year ago, I’ve written a post about Managing WPF resources contained in external assemblies . I am

# October 19, 2010 1:35 PM

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: