6

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 ?

AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84
  • Are you asking why the XAML in the separate file still needs an x:Key? – BradleyDotNET Jul 17 '14 at 00:33
  • @BradleyDotNET : No, as I mentioned, for the element that contain the – AymenDaoudi Jul 17 '14 at 00:35
  • Just to clarify, *exactly* which element do you think shouldn't have a key on it? Show the tag, or some other way that is so specific that I will know which one you are referring to :) – BradleyDotNET Jul 17 '14 at 00:36
  • Got it, I saw the error there but wanted to make sure that was what you were talking about. Just rearrange a little bit and you should be good (see my answer). – BradleyDotNET Jul 17 '14 at 00:49

1 Answers1

5

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 :)

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117