locked
How do you reference a merged ResourceDictionary? RRS feed

  • Question

  • Okay, I have a bunch of resource dictionaries that contains styles, templates, custom brushes, etc. in a shared library called "CustomLibrary".

    In my App.xaml, I was merging them all like this:

    <Application
      x:Class="XXX.XXX.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      StartupUri="MainWindow.xaml">
      <Application.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/BrushDictionary.xaml" />
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/TextBlockDictionary.xaml" />
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/LabelDictionary.xaml" />
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/ButtonDictionary.xaml" />
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/ListViewDictionary.xaml" />
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/GridViewDictionary.xaml" />
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/GridDictionary.xaml" />
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/WindowDictionary.xaml" />
          </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
      </Application.Resources>
    </Application>


    This works fine. All the resources inside the individual dictionaries can be referred to by any Window in my application.

    But then I added another project to my solution, and I realized I'd like it to have access to all the same resources. I decided to add another ResourceDictionary file to my libarary called CombinedDictionary.xaml, which contains the following:

    <ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="BrushDictionary.xaml" />
        <ResourceDictionary Source="TextBlockDictionary.xaml" />
        <ResourceDictionary Source="LabelDictionary.xaml" />
        <ResourceDictionary Source="ButtonDictionary.xaml" />
        <ResourceDictionary Source="ListViewDictionary.xaml" />
        <ResourceDictionary Source="GridViewDictionary.xaml" />
        <ResourceDictionary Source="GridDictionary.xaml" />
        <ResourceDictionary Source="WindowDictionary.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>


    I thought this would allow me to refer to all my resources with just one line of xaml. This way, if I added any new ResourceDictionary files to the collection, I would just add them to the CombinedDictionary, and all my apps that use these resources would automatically have access.

    So I went ahead and changed my App.xaml to this:

    <Application
      ...>
      <Application.Resources>
        <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/CombinedDictionary.xaml" />
      </Application.Resources>
    </Application>


    This compiled successfully but when I tried to run it, the following exception was thrown:

     {"Cannot find resource named '{_fadedOrangeBrush}'. Resource names are case sensitive.  Error at object 'System.Windows.Setter' in markup file 'CustomLibrary;component/ResourceDictionaries/WindowDictionary.xaml'."}

    (Note: "_fadedOrangeBrush" is the first StaticResource requested by the Window.)

    I also tried the following:

    <Application
      ...>
      <Application.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/CustomLibrary;component/ResourceDictionaries/CombinedDictionary.xaml" />
          </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
      </Application.Resources>
    </Application>


    Same result. It can't find anything.

    So, my question is:

    Is it possible to refer to a ResourceDictionary file that contains references to *other* ResourceDictionary files? If yes, how?

    Thanks very much,

    -Dan
    Tuesday, April 8, 2008 9:59 PM

Answers

  • It actually seems like what you're doing should work in theory.  I had to do something similar with my project - I created a resource dictionary that merges a bunch of other dictionaries ... and then merge the parent resource dictionary into my other application's resource file ... the difference is I did it in code.  Like so:

     

    Code Snippet

    ResourceDictionary dict = new ResourceDictionary();

    dict.Source = new Uri("pack://application:,,,/MyAssemblyName;component/themes/generic.xaml");

    Application.Current.Resources.MergedDictionaries.Add(dict);

     

     

     

    The generic.xaml file referenced above is the "parent" resource dictionary that contains all my other resource dictionaries merged in.

    Wednesday, April 9, 2008 12:11 AM
  • Do you mean that you want to use other resources defined in other resource dictionaries? If so, you should use merged dictionary to do this. We are unable to defined resources in App.xaml and use them in any way.

     

    Code Snippet

    <ResourceDictionary

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="BrushDictionary.xaml"/>

        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">

            <Setter Property="Template">

                <Setter.Value>

                    <ControlTemplate TargetType="{x:Type Button}">

                        <Border BorderBrush="Beige" Background="{StaticResource BackgroundBrush}">

                            <ContentPresenter/>

                        </Border>

                    </ControlTemplate>

                </Setter.Value>

            </Setter>

        </Style>

    </ResourceDictionary>

     

     

    Best Regards,

    Wei Zhou

    Thursday, April 10, 2008 2:27 AM

All replies

  • It actually seems like what you're doing should work in theory.  I had to do something similar with my project - I created a resource dictionary that merges a bunch of other dictionaries ... and then merge the parent resource dictionary into my other application's resource file ... the difference is I did it in code.  Like so:

     

    Code Snippet

    ResourceDictionary dict = new ResourceDictionary();

    dict.Source = new Uri("pack://application:,,,/MyAssemblyName;component/themes/generic.xaml");

    Application.Current.Resources.MergedDictionaries.Add(dict);

     

     

     

    The generic.xaml file referenced above is the "parent" resource dictionary that contains all my other resource dictionaries merged in.

    Wednesday, April 9, 2008 12:11 AM
  • Do you mean that you want to use other resources defined in other resource dictionaries? If so, you should use merged dictionary to do this. We are unable to defined resources in App.xaml and use them in any way.

     

    Code Snippet

    <ResourceDictionary

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

        <ResourceDictionary.MergedDictionaries>

            <ResourceDictionary Source="BrushDictionary.xaml"/>

        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">

            <Setter Property="Template">

                <Setter.Value>

                    <ControlTemplate TargetType="{x:Type Button}">

                        <Border BorderBrush="Beige" Background="{StaticResource BackgroundBrush}">

                            <ContentPresenter/>

                        </Border>

                    </ControlTemplate>

                </Setter.Value>

            </Setter>

        </Style>

    </ResourceDictionary>

     

     

    Best Regards,

    Wei Zhou

    Thursday, April 10, 2008 2:27 AM