Problem with a resource dictionary link that contains a federated dictionary

I have a CommonLibraryWpfThemes library with multiple XAML files in a resource dictionary. My Themes / Generic.xml contains a ResourceDictionary.MergedDictionaries declaration that combines all other files.

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 link 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 does not work. When starting the application, the following error appears:

 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 in 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> 



Perhaps I will change it wrong. My goal is to simplify the link to all my thematic resources from several applications without having to list all the individual files. Is there a recommended way to do this? (Note: I'm not trying to switch between multiple topics - I only have one topic.)

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

Thank.

EDIT:

I tried to wrap a ResourceDictionary in a ResourceDictionary.MergedDictionary element, but this also did not 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> 
+41
wpf xaml themes resourcedictionary
Aug 04 '09 at 19:18
source share
5 answers

The answer to a similar question is here earlier, see Adding a federated dictionary to a federated dictionary .

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

About creating each object in XAML, if there is a default style (i.e. a style with a type key), which style should be applied. As you can imagine, there are several characteristics of optimization, to do this (implied) find light weight as much as possible. One of them is that we do not look inside the Resource Dictionaries unless they are marked as “containing the default Styles”. There is a mistake: if all your styles are, by default, nested in combined dictionaries at three levels (or deeper), the top dictionary will not get a flag so that the search skips it. The work around is to have the default Style for something, anything, in the root dictionary.

Thus, 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> 
+64
Nov 12 2018-10-12
source share

Test your constructor in the App.xaml.cs calls to InitializeComponent () - this is what combines resource dictionaries ...

+12
Jan 19 '10 at 11:20
source share

You do not need to reference generic.xaml at all, it has built-in support. This, however, means that it provides a default style that you do not explicitly set. Explicit styles / templates should be accessible from explicit dictionaries.

(EDIT for clarity)

The one exception is App.xaml , where certain resources become available for the entire application, without having to refer to any particular resource dictionary. The resource itself must be available by name.

The reason this fails

 <Application.Resources> <ResourceDictionary Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" /> </Application.Resources> 

I think because you did not wrap it in a MergedDictionary wrapper by adding it to merged dictionaries. Adding directly to resources only works for resources that you declare locally, for example. styles themselves, etc.

However, as I said, you don’t need to combine generic.xaml anywhere, maybe you just need to reorganize brushes and other resources used outside of the styles and combine only those resources in App.xaml .

Also note that styles do not have to be in generic.xaml in order to have a default behavior - if a style with a key equal to the element type (globally or in local resources) is available for it, then it will use the style as the default style. generic.xaml is just a convenience.

Check this answer.

For other custom brushes, etc. You must explicitly reference these resources.

You should also check the contents of WindowDictionary.xaml , this error has a certain smell.

+4
Aug 04 '09 at 19:32
source share

I was getting this error in my unit tests, and Chris's answer from above gave me the key I need. Basically on my first tested method, I set:

  MyApplication.App app = new MyApplication.App(); app.InitializeComponent(); 

And suddenly he was able to find my template for my pages. Note: this means you need to check if an instance of your application exists if you are also testing the App.cs device.

0
Nov 20
source share

My solution is here , click "Workarounds."

-one
Oct. 15 '10 at 11:13
source share



All Articles