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

时间:2010-08-05 21:58:59

标签: wpf silverlight xaml resourcedictionary

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

Theme.xaml

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Mine;component/Themes/Palette.Blue.xaml"/>
    <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
</ResourceDictionary.MergedDictionaries>

申请资源

<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>

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

3 个答案:

答案 0 :(得分:35)

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

  

关于创建每个对象   XAML,如果存在默认样式   (即具有类型的键的样式)   风格应该适用。尽你所能   想象有几个表现   优化(隐含)   查找尽可能轻的重量。一   他们是我们不看里面   资源字典除非它们是   标记为“包含默认值   样式”。有一个错误:如果你所有的   默认样式嵌套在合并中   字典深入三层(或   更深层次的顶级词典没有   得到标记,以便搜索跳过它。   解决方法是设置默认值   在根中设置某种东西,任何东西   字典。

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

<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>

答案 1 :(得分:0)

您的示例代码在Palette.Blue.xaml的App.xaml合并资源字典源中具有双等号。我假设这是你在这里发布的例子的拼写错误,而不是你真正的问题。

弄清楚如何直接在XAML中链接所有资源可能很棘手。最简单的方法是在Blend的Resources面板中。我创建了一个Silverlight应用程序,其资源文件名称与您的示例类似,然后在Blend中打开项目并快速将它们链接在一起。

<强>的App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightApplication1.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Theme.xaml" />
                <!--
                <ResourceDictionary Source="Palette.Blue.xaml"/>
                <ResourceDictionary Source="Template.xaml"/>
                -->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

<强> Theme.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Palette.Blue.xaml"/>
        <ResourceDictionary Source="Template.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

<强> Template.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="TextBox">
        <Setter Property="Margin" Value="10" />
        <Setter Property="Width" Value="250" />
    </Style>
    <Style x:Key="ReadOnlyTextBoxStyle" TargetType="TextBox">
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Margin" Value="10" />
        <Setter Property="Width" Value="250" />
    </Style>
</ResourceDictionary>

<强> Palette.Blue.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="BlueSolidColorBrush" Color="SkyBlue" />
</ResourceDictionary>

<强> MainPage.xaml中

<UserControl x:Class="SilverlightApplication1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel x:Name="LayoutRoot" Background="Honeydew">
        <TextBox Text="Read Only Textbox"
                 Style="{StaticResource ReadOnlyTextBoxStyle}" />
        <TextBox Text="Blue Textbox"
                 Background="{StaticResource BlueSolidColorBrush}" />
        <TextBox Text="Read Only, Blue Textbox"
                 Style="{StaticResource ReadOnlyTextBoxStyle}"
                 Background="{StaticResource BlueSolidColorBrush}" />
    </StackPanel>
</UserControl>

当然,如果您要链接来自不同程序集的资源,它们看起来会有所不同。实际上,在这种情况下,我建议考虑在后面的代码中合并你的词典。

答案 2 :(得分:0)

如果在您自己的某个控件上发生这种情况,我发现另一个解决方案是将DefaultStyleKey属性设置为null:

DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(null));

我不知道为什么会这样,但似乎!