我在名为ResourceDictionary
的单独文件中有一个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>
我将此ResourceDictionary放在MergedDictionaries
App.Xaml
Application.Resources
中,如下所示:
<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不会停止要求包含ResourceDictionary的x:Key(包含<ResourceDictionary.MergedDictionaries>
的那个),你能解释一下为什么,我应该怎么做?
答案 0 :(得分:4)
Visual Studio想要一个关键的&#34;合并&#34; ResourceDictionary
因为Resources
集合本身是ResourceDictionary
,ResourceDictionary
(或任何字典)中的每个项目都必须包含<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>
一把钥匙。
通常情况下,你会写下你喜欢的内容:
ResourceDictionary
这会将隐式 MergedDictionaries
设置为显式,然后按预期设置ResourceDictionary
属性。由于您没有向隐式密钥添加新的{{1}},因此它不需要单独的密钥。这种方法还有一个额外的好处,就是实际做你想做的事情:)