将合并字典添加到合并字典

The*_*ann 21 silverlight wpf xaml resourcedictionary

我似乎无法将合并的字典添加到XAML中的合并字典集合中.

Theme.xaml

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Mine;component/Themes/Palette.Blue.xaml"/>
    <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)

应用资源

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/> 
            <!--
            <ResourceDictionary Source=="/Mine;component/Themes/Palette.Blue.xaml"/>
            <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
            -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

注意:如果我将两个ResourceDictionaries放在Appication.Resources MergedDictionary中(注释掉theme.xaml并取消注释其他两个词典),它们都会正确加载.但是,我们的资源定义方式,这可能意味着将加载相当多的资源,而对于动态加载,我希望能够定义模板.

Fre*_*lad 35

这是一个优化错误,请参阅链接

在XAML中创建每个对象时,如果存在默认样式(即具有Type的键的样式),则应该应用该样式.您可以想象,有几种性能优化可以使(隐含)查找尽可能轻量级.其中之一是我们不会查看资源字典,除非它们被标记为"包含默认样式".有一个错误:如果所有默认样式都嵌套在合并的字典中三个级别(或更深),则顶级字典不会被标记,因此搜索会跳过它.解决方法是在根词典中将默认样式放在某个东西上.

因此,在根字典中添加虚拟样式可以解决此问题.例

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <!-- Dummy Style, anything you won't use goes -->
        <Style TargetType="{x:Type Rectangle}" />
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

  • 出于某种原因,这对我不起作用.启动时,它会抛出一个异常(DependencyProperty.UnsetValue对'Foreground'无效),表示没有添加资源字典.我的设置与.NET 4.5上面的代码完全相同. (4认同)