none
Merging resource dictionaries in a WPF User Control Class Library project

    Question

  • I am have a big WPF Application (WPFLauncher.exe). This application loads other class libraries using ServiceLocator (Prism concept). Hence I never have to reference any class library dll in WPFLauncher.exe.

    I have a ResourceDictionary "GenericStyleSheet.xaml" in WPFLancher.exe. The App.xaml for WPF Application looks like this:

        <Application x:Class="WPFLauncher.App"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
            <Application.Resources>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="pack://application:,,,/WPFLauncher;component/Resources/Dictionary/GenericStyleSheet.xaml"/>
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </Application.Resources>
        </Application>
    I have a HelloWorld.dll which is a wpf class library project and is *not* referenced in WPFLauncher.exe. This project has many (50+) usercontrols. In HelloWorld.dll I have added another ResourceDictionary called "HelloWorldStyleSheet.xaml". 

    I have many such HelloWorld projects which have many usercontrols and their own StyleSheets.

    I want to merge "GenericStyleSheet.xaml" and "HelloWorldStyleSheet.xaml" in **HelloWorld.dll**, so I created a new ResourceDictionary like this "HelloWorldMergedStyleSheet.xaml" (build action for this is set to Page):

        
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                            xmlns:local="clr-namespace:WPFCustomUC.Resources.Dictionary">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WPFLauncher;component/Resources/Dictionary/GenericStyleSheet.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/WPFCustomUC;component/Resources/Dictionary/HelloWorldStyleSheet.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    


    **Problem Statement**: HelloWorldMergedStyleSheet.xaml is never loaded.

    **Potential Solutions**:

    1. Brute Force: In every user control in HelloWorld.dll, use the following code:

           
     <UserControl.Resources>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
        			<ResourceDictionary Source="pack://application:,,,/WPFLauncher;component/Resources/Dictionary/GenericStyleSheet.xaml"/>
        			<ResourceDictionary Source="pack://application:,,,/WPFCustomUC;component/Resources/Dictionary/HelloWorldStyleSheet.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </UserControl.Resources>
        
    This works, but I do not want to add this in my 50+ usercontrols.

    2. Merging ResouceDictionaries in WPFLauncher's App.xaml

       
    <Application x:Class="WPFLauncher.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
            <Application.Resources>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
        				<ResourceDictionary Source="pack://application:,,,/WPFLauncher;component/Resources/Dictionary/GenericStyleSheet.xaml"/>
        				<ResourceDictionary Source="pack://application:,,,/WPFCustomUC;component/Resources/Dictionary/HelloWorldStyleSheet.xaml"/>
        			</ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </Application.Resources>
        </Application>

    This doesn't work becase HelloWorld.dll is *not* loaded when WPFLauncher's app.xaml is loaded.

    **Is there a better way?**

    Thanks,

    RDV

    Wednesday, January 03, 2018 6:30 PM

All replies

  • Hi,

    Unless there is a way which i do not know, I am afraid you have to add this ResouceDictionaries for every usercontrol.

    Sincerely,

    Bob 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 04, 2018 8:44 AM
    Moderator
  • I'm not at all clear about how these usercontrols are used.

    You can merge resource dictionaries in code.

    Whatever you merge at application level goes into application.current.resources.

    You could therefore load your base resource dictionary using app.xaml.

    Each time you use a control library you could merge their resource dictionary in code.

    You might of course then need to clear resources and re load the base when you decide to use a different library.

    Resources have scope.

    Maybe you're using your usercontrols in a window or contentcontrol.

    You could merge your resource dictionary at window level and then whichever usercontrols are used in that window will get the stuff from that as resources.

    Maybe usercontrols are in the same scope but you can define unique resources so usercontrol x and usercontrol Y from the library XXX use brush XXXWhatever and other usercontrols from library YYY use brush YYYWhatever.

    If instead you have usercontrol X from one library and usercontrol Y from another and they're used in the same scope and you want different resources for each without worrying about unique keys then you will have to load those resources at usercontrol level.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Thursday, January 04, 2018 11:05 AM
    Moderator