将合并词典添加到合并词典

问题描述 投票:0回答:3
wpf silverlight xaml resourcedictionary
3个回答
35
投票

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

关于每一个对象的创建 XAML,如果存在默认样式 (即带有 Type 键的样式) 应应用样式。尽你所能 想象有几种表现 优化(暗示) 查找尽可能轻的重量。一 其中之一就是我们不往里看 资源字典,除非它们是 标记为“包含默认值 样式”。有一个错误:如果你所有的 默认样式嵌套在 merged 中 字典深三层(或 更深)顶级词典没有 被标记以便搜索跳过它。 解决方法是设置一个默认值 对某事、任何事物的风格,在根 词典.

所以向根字典添加虚拟样式可以解决这个问题。例子

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

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

弄清楚如何在 XAML 中直接链接所有资源可能很棘手。最简单的方法是使用 Blend 中的资源面板。我用你的例子命名的资源文件创建了一个 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>

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


0
投票

如果这发生在您自己的一个控件上,我发现另一种解决方案是将

DefaultStyleKey
属性设置为 null:

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

我不知道为什么这行得通,但似乎行得通!

© www.soinside.com 2019 - 2024. All rights reserved.