我在另一个名为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>的ResourceDictionary)的x:Key,您能向我解释为什么吗,我该怎么办?

最佳答案

Visual Studio希望在“合并的” 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属性。由于您没有在隐式代码中添加新的ojit_code,因此不需要单独的 key 。这种方法还具有实际执行您想要的效果的额外好处:)

关于c# - 为什么Application.Resources中的ResourceDictionary需要x :Key,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24793043/

10-15 04:57