Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble referencing a Resource Dictionary that contains a Merged Dictionary

I have a library, CommonLibraryWpfThemes, with several Resource Dictionary XAML files in it. My Themes/Generic.xml file contains a ResourceDictionary.MergedDictionaries declaration that merges all the other files together.

Generic.xaml

<ResourceDictionary     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <ResourceDictionary.MergedDictionaries>         <ResourceDictionary             Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />         <ResourceDictionary             Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />         <ResourceDictionary             Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />         <ResourceDictionary             Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />         <ResourceDictionary             Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />     </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

In my application project, I have a reference to CommonLibraryWpfThemes, and I explicitly reference Generic.xml in my App.xaml file.

App.xaml -- FAILS

<Application     x:Class="MyApp.App"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <Application.Resources>         <ResourceDictionary             Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />     </Application.Resources> </Application> 

This doesn't work. I get the following error when I run my app:

System.Windows.Markup.XamlParseException occurred   Message="Cannot find resource named '{_fadedOrangeBrush}'. Resource names are case sensitive.  Error at object 'System.Windows.Setter' in markup file 'CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml' Line 18 Position 13."   Source="PresentationFramework"   LineNumber=18   LinePosition=13 

If I place the contents of Generic.xaml into App.xaml directly, everything works fine:

App.xaml -- SUCCEEDS

<Application     x:Class="MyApp.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="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />                 <ResourceDictionary                     Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />                 <ResourceDictionary                     Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />                 <ResourceDictionary                     Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />                 <ResourceDictionary                     Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />             </ResourceDictionary.MergedDictionaries>         </ResourceDictionary>     </Application.Resources> </Application> 

Maybe I'm going about this in the wrong way. My goal is to make it easy to reference all my theme resources from multiple applications without having to list out all the individual files. Is there a recommended way to do this? (Note: I'm not trying to switch between multiple themes--I just have one theme.)

As a bonus, it would be nice if someone could tell me how to reference resources in an external library without breaking the designer in Visual Studio.

Thanks.

EDIT:

I tried wrapping the ResourceDictionary in a ResourceDictionary.MergedDictionary element, but that also didn't work (I get the same error):

<Application     x:Class="MyApp.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="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />             </ResourceDictionary.MergedDictionaries>         </ResourceDictionary>     </Application.Resources> </Application> 
like image 597
devuxer Avatar asked Aug 04 '09 19:08

devuxer


People also ask

How do you reference a resource dictionary?

You can reference a resource throughout an app or from any XAML page within it. You can define your resources using a ResourceDictionary element from the Windows Runtime XAML. Then, you can reference your resources by using a StaticResource markup extension or ThemeResource markup extension.

What is ResourceDictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.

What all we can store in a resource dictionary?

In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax.


1 Answers

Answered a similar question here earlier, see Adding a Merged Dictionary to a Merged Dictionary question.

This is an optimization bug, see Microsoft Connect / DefaultStyleKey style not found in inner MergedDictionaries:

On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible. One of them is that we don’t look inside Resource Dictionaries unless they are flagged as “containing default Styles”. There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

So adding a dummy style to the root dictionary fixes this. Example

<Application x:Class="MyApp.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="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />                  </ResourceDictionary.MergedDictionaries>              <!-- Dummy Style, anything you won't use goes -->              <Style TargetType="{x:Type Rectangle}" />          </ResourceDictionary>      </Application.Resources>  </Application>    
like image 164
Fredrik Hedblad Avatar answered Nov 08 '22 04:11

Fredrik Hedblad