[英]Why is the ResourceDictionary in Application.Resources requiring a x:Key
I have a ResourceDictionary
in a separate file called MainSkin.xaml
: 我在一个名为MainSkin.xaml
的单独文件中有一个ResourceDictionary
:
<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 : 我将这个ResourceDictionary放在App.Xaml
Application.Resources
中的MergedDictionaries
中,如下所示:
<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 ax:Key for the containing ResourceDictionary (the one that contains the <ResourceDictionary.MergedDictionaries>
), can you explain to me why, and what should I do ? Visual Studio不会停止询问ax:包含ResourceDictionary的键(包含<ResourceDictionary.MergedDictionaries>
),你能解释一下为什么,我该怎么办?
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. Visual Studio需要一个关于“合并” ResourceDictionary
的键,因为Resources
集合本身是一个ResourceDictionary
,而ResourceDictionary
(或任何字典)中的每个项目都必须有一个键。
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. 这会将隐式 ResourceDictionary
设置为显式的 ,然后按预期设置MergedDictionaries
属性。 Because you are not adding a new ResourceDictionary
to the implicit one, it doesn't require a separate key. 因为您没有向隐式ResourceDictionary
添加新的ResourceDictionary
,所以它不需要单独的密钥。 This method has the added benefit of actually doing what you intend as well :) 这种方法还有额外的好处,实际上做你想要的:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.