Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the ResourceDictionary in Application.Resources requiring a x:Key

I have a ResourceDictionary in a separate file called MainSkin.xaml :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="RoundedButton">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Viewbox>
                        <Grid>
                            <Grid Name="backgroundGrid" Width="80" Height="80" Visibility="Visible">
                                <Path Data="Some Data Path here" Stretch="Fill" Fill="#FFFFFFFF" Name="Stroke" Visibility="Visible" />
                            </Grid>
                            <Path Data="Some Data Path here" Stretch="Uniform" Fill="#FFF9F9F9" Width="44" Height="44" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
                                <Path.RenderTransform>
                                    <TransformGroup>
                                        <TransformGroup.Children>
                                            <RotateTransform Angle="0" />
                                            <ScaleTransform ScaleX="1" ScaleY="1" />
                                        </TransformGroup.Children>
                                    </TransformGroup>
                                </Path.RenderTransform>
                            </Path>
                        </Grid>
                    </Viewbox>    
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

and I'm putting this ResourceDictionary in MergedDictionaries in the App.Xaml Application.Resources as follows :

<Application.Resources>
        <!--Global View Model Locator-->
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True" />

        <-- VS is asking for a x:key here, why ? --/>
        <ResourceDictionary ----> x:Key="" <-----     >
            <ResourceDictionary.MergedDictionaries>
                 <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

</Application.Resources>

Visual Studio doesn't stop asking for a x:Key for the containing ResourceDictionary (the one that contains the <ResourceDictionary.MergedDictionaries>), can you explain to me why, and what should I do ?

like image 482
AymenDaoudi Avatar asked Jul 17 '14 00:07

AymenDaoudi


People also ask

How do you reference a resource dictionary?

You can reference a resource throughout an app or from any XAML page within it. You can define your resources using a ResourceDictionary element from the Windows Runtime XAML. Then, you can reference your resources by using a StaticResource markup extension or ThemeResource markup extension.


1 Answers

Visual studio wants a key on your "merged" ResourceDictionary because the Resources collection itself is a ResourceDictionary, and every item in a ResourceDictionary (or any dictionary, for that matter) must have a key.

Normally, you would write what you have like this:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <!--Global View Model Locator-->
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True" />
    </ResourceDictionary>
</Application.Resources>

This sets the implicit ResourceDictionary to an explicit one, then sets the MergedDictionaries properties as you expect. Because you are not adding a new ResourceDictionary to the implicit one, it doesn't require a separate key. This method has the added benefit of actually doing what you intend as well :)

like image 171
BradleyDotNET Avatar answered Oct 05 '22 11:10

BradleyDotNET