Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 854
Rodney Avatar asked Sep 29 '10 00:09

Rodney


2 Answers

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

like image 92
chadbr Avatar answered Nov 15 '22 22:11

chadbr


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

like image 44
Rodney Avatar answered Nov 15 '22 20:11

Rodney