Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Merged Dictionary to a Merged Dictionary

Tags:

I can't seem to be able to add a merged dictionary to a collection of merged dictionaries within XAML.

Theme.xaml

<ResourceDictionary.MergedDictionaries>     <ResourceDictionary Source="/Mine;component/Themes/Palette.Blue.xaml"/>     <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/> </ResourceDictionary.MergedDictionaries> 

Application Resources

<Application.Resources>     <ResourceDictionary>         <ResourceDictionary.MergedDictionaries>             <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/>              <!--             <ResourceDictionary Source=="/Mine;component/Themes/Palette.Blue.xaml"/>             <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>             -->         </ResourceDictionary.MergedDictionaries>     </ResourceDictionary> </Application.Resources> 

Note: if I put both ResourceDictionaries in the Appication.Resources MergedDictionary (comment out the theme.xaml and uncomment the other two dictionaries) they both load correctly. However, the way our resources are defined, this may mean that quite a few resources will be loaded, and for dynamic loading I'd like to be able to define templates.

like image 505
The Real Baumann Avatar asked Aug 05 '10 21:08

The Real Baumann


1 Answers

This is an optimization bug, see this link

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.Resources>     <ResourceDictionary>         <ResourceDictionary.MergedDictionaries>             <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/>         </ResourceDictionary.MergedDictionaries>         <!-- Dummy Style, anything you won't use goes -->         <Style TargetType="{x:Type Rectangle}" />     </ResourceDictionary> </Application.Resources> 
like image 122
Fredrik Hedblad Avatar answered Sep 25 '22 11:09

Fredrik Hedblad