Silverlight 4 - use StaticResource from one ResourceDictionary to another
If I have these dictionaries:
dict1.xaml: <Color x:Key="Color1">Red</Color>
dict2.xaml: <SolidColorBrush x:Key="Brush1" Color={StaticResource Color1} />
It works:
App.xaml:
<MergedDictionaries> <ResourceDictionary Source="dict1.xaml"/> <MergedDictionaries> SomePage.xaml:
<UserControl.Resources> <MergedDictionaries> <ResourceDictionary Source="dict2.xaml"/> </MergedDictionaries> </UserControl.Resources> Is not:
App.xaml integrates at the application level.
I get a message that Color1 was not found.
Why is this? / Is there a way around this? I know this example is simplified, but the real one will be too long. Basically, I'm just trying to organize my styles and patterns in different files:
- One for flowers
- One for implicit styles
- Many for explicit styles.
edit: curious if I do this in Application_Startup code, before setting the RootVisual property I don't get an error ... I just wonder why!
Unfortunately, you are faced with an annoying limitation in the Silverlight resource system, which I can only imagine about - this is an optimization problem. There seems to be some kind of asynchronous behavior where each dictionary in MergedDictionaries loaded in parallel, so when loading "dict2.xaml" you cannot rely on the contents of "dict1.xaml".
The simplest solution is to incorporate the merge of Dict1 into Dict2: -
App.xaml:
<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="dict2.xaml" /> <ResourceDictionary.MergedDictionaries> Dict2.xaml:
<ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="dict1.xaml" /> </ResourceDictionary.MergedDictionaries> .... <!-- dict2 resource --> </ResourceDictionary> It will be in App.xaml
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="dict1.xaml" /> <ResourceDictionary Source="dict2.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> as long as you define a dictionary in front of another, I'm a little surprised that something similar to the above will not work.
You can do it in the way suggested by @Anthony, but there is one caveat here - if you use your 1st resource dictionary, for example, in 5 other dictionaries, it will be downloaded 5 times and you will have 6 copies of the same dictionary resources (this is if you referred to it in App.xaml), which is not very good for performance. This can be fixed using a small subclass of the Silverlight resource dictionary from here - http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/ .
I do this. "The logic of finding resources in a collection of consolidated resource dictionaries in the past, in the first place." In other words, if you have several dictionaries where one refers to another, the dictionary containing the referenced resources should be on the top of the stack. Make sure that you refer to them in the correct order and be careful that the dictionaries at the top of the stack are not dependent on the dictionaries at the bottom of the stack.
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Dict1.xaml" /> <ResourceDictionary Source="dependsOnDict1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>