Trying to use a ResourceDictionary, but styles appear in it as not found

I have a Silverlight class library called MyClassLibrary.

In it, I have a custom MyControl control.

Inside the control, I define user resources:

<UserControl.Resources> <Style x:Key="ComboBoxStyle" TargetType="ComboBox"> (lots of xaml) </Style> </UserControl.Resources> 

The control consumes this style:

 <ComboBox Style="{ StaticResource ComboBoxStyle }"></ComboBox> 

This all works great, ComboBox is right style, so I know the style is spelled correctly.

I really want the style to be in the resource dictionary so that it can be used by several different controls in this assembly. Therefore, I create a resource dictionary in the SAME assembly. I call it ResourceDictionary.xaml.

I am transferring a style definition from my user control to a resource dictionary.

So, the resource dictionary is as follows:

 <ResourceDictionary xmlns="etc" > <Style x:Key="ComboBoxStyle" TargetType="ComboBox"> (lots of xaml) </Style> </ResourceDictionary> 

User management resources now look like this:

 <UserControl.Resources> <ResourceDictionary Source="/MyClassLibrary;component/ResourceDictionary.xaml" x:Name="resDict"/> </UserControl.Resources> 

And the control still consumes the style exactly the same as before.

Now I know that he finds the ResourceDictionary.xaml file because I tried to change the Source attribute to NonExistentFile.xaml and he complained that he could not find the file. He does not make this complaint with ResourceDictionary.xaml, so I assume she will find it.

However, when I launch the application, I get the error message "Cannot find the resource with the name / key of ComboBoxStyle."

What am I doing wrong? It seems so simple and doesn't work.

Thanks in advance for any help you can give me.

+4
source share
2 answers

Not sure if this helps for sure, but I am including my ResourceDictionaries in App.xaml:

 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Sausage/Bangers.xaml"/> <ResourceDictionary> .. other stuff, eg <helpers:NotOperatorValueConverter x:Key="NotOperatorValueConverter" /> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 

Even if you don't like this approach, you can see that my Source = is different from yours.

+2
source

Thank you both, your answers really allowed me to solve this problem.

I really had something like this:

 <UserControl.Resources> <Style ...> stuff </Style> </UserControl.Resources> 

To which I then added, so that it looks like this:

 <UserControl.Resources> <Style ...> stuff </Style> <ResourceDictionary Source="..." /> </UserControl.Resources> 

Now it compiled very nicely, but just doesn't start. I did not understand that I needed to use MergedDictionaries. Then I got this concept and reorganized the section, and now it all works great.

Many thanks!

0
source

Source: https://habr.com/ru/post/1299790/


All Articles