6

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(包含 的那个<ResourceDictionary.MergedDictionaries>)的 ax:Key,你能解释一下为什么,我应该怎么做?

4

1 回答 1

5

Visual Studio 想要一个“合并”的键,ResourceDictionary因为Resources集合本身是 a ResourceDictionary,并且 a 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隐式添加新的,所以它不需要单独的键。这种方法还有一个额外的好处,那就是实际做你想做的事情:)

于 2014-07-17T00:48:36.837 回答