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 ?
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With