How to define resources AND a MergeDictionary with a key in a SL4 page
This开发者_运维百科 is probably a really stupid question but I can't figure this out.
I have a page with a MergeDictionary defined:
<navigation:Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</navigation:Page.Resources>
and I reference the styles in TourneySetupStyles.xaml in my XAML like this with no problem:
<TextBlock Text="Tourney Name:" Style="{StaticResource TourneySetupTextStyle}" />
However, now I need to add another page resource like this:
But the designer now throws a warning: "The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection."
So I add a key to my ResourceDictionary like this:
<navigation:Page.Resources>
<local:Tournament x:Key="tournament" />
<ResourceDictionary x:Key="whatever">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</navigation:Page.Resources>
and the warning goes away. BUT now my reference to the style in TourneySetupStyles no longer works:
"Cannot find a Resource with the Name/Key TourneySetupTextStyle"
So I guess the question is: How do I access the style now that the ResourceDictionary is keyed?
I just ran into this today -- I'm cross compiling to WPF / Silverlight. Put the local resource inside the ResourceDictionary node, don't put a x:Key on the ResourceDictionary node.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/mydll;component/folder/MyResDict.xaml" />
</ResourceDictionary.MergedDictionaries>
<LinearGradientBrush x:Key="OrangeGradient"
EndPoint="0.5,1"
MappingMode="RelativeToBoundingBox"
StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<RotateTransform CenterY="0.5"
CenterX="0.5" />
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="#FFF3801E" />
<GradientStop Color="#FFEDB17E"
Offset="0.5" />
<GradientStop Color="#FFF3801E"
Offset="1" />
</LinearGradientBrush>
</ResourceDictionary>
</UserControl.Resources>
I can't explain why - but I know it works...
hth
sigh it seems that the order of the declarations is important, as soon as I move the first resource down it now works:
<navigation:Page.Resources>
<ResourceDictionary x:Key="TourneySetupStyles">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Assets/TourneySetupStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<local:Tournament x:Key="tourneySetupViewModel" />
</navigation:Page.Resources>
If anyone can explain why it would be great for future reference...
I came across the same problem.
I resolved the issue be defining my dictionary merges within the xaml file of the application instead of the view itself.
Ex:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\Brushes\Brushes_Dictionary.xaml" />
<ResourceDictionary Source="Resources\Storyboards\Storyboard_Dictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Yes, I've just been bitten by this. As soon as Silverlight loads the merged resource dictionary, it deletes all local resources you've already added! In my case I need to programmatically add a resource before the InitalizeComponent() call, but since the UserControl includes a merged ResourceDictionary that resource is lost. IMHO this is a bug in Silverlight.
But putting local resources after the ResourceDictionary will work for cases like yours so I've up voted it.
精彩评论