简体   繁体   English

为什么Application.Resources中的ResourceDictionary需要x:Key

[英]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.

相关问题 如何在App.xaml文件的Application.Resources中组合LocalizedStrings和ResourceDictionary标记 - How to Combine LocalizedStrings and ResourceDictionary tags in the Application.Resources of the App.xaml file Application.Resources中的多个条目 - Multiple entries in Application.Resources 为什么我可以更改在 Windows.Resources 而不是从 Application.Resources 定义的资源属性 - Why I can change a resource properties when it defined at Windows.Resources but not from Application.Resources 如何在Application.Resources中上标字符串 - How to superscript a string in Application.Resources 如何将Application.Resources移至外部程序集? - How to move Application.Resources to external assembly? 通过C#设置Application.Resources - Setting Application.Resources through C# 在自定义应用程序中访问ControlTemplate样式。 - Accessing ControlTemplate Style In Custom Application.Resources 逻辑删除后是否重新创建Application.Resources? - Are Application.Resources recreated after tombstoning? 为什么Freezables已经冻结并且存储在Application.Resources中时具有null Dispatcher(与Styles相同)? - Why are Freezables already frozen and have a null Dispatcher (same with Styles) when stored in Application.Resources? 我可以存储字符串吗? <Application.Resources> 并绑定它? - Can i store string in <Application.Resources> and bind to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM