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

标签 c# wpf visual-studio xaml xaml-resources

我有一个 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> 的那个)请求 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,所以它不需要单独的键。这种方法还有一个额外的好处,就是可以按照您的意愿进行实际操作:)

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

相关文章:

c# - 使用 MVVM 通过 ViewModel 属性冒泡 INotifyPropertyChanged 事件的好方法是什么?

c# - 创建用户控件时是否应该尝试遵循 MVVM

c# - SQLite-net Table<>()方法调用性能

c# - Silverlight 中的动态数据输入表单

c# - 如何告诉谷歌我有另一个页面?

c# - 限制对某些 .aspx 页面的访问

wpf - CurrentCell 更改为 Datagrid 中不是 SelectedItem 的行

c++ - 程序在 visual studio 中运行但释放可执行文件崩溃

c++ - 替代 C/C++ localtime()

c# - "Nullable object must have a value"当Where使用方法时?