【问题标题】:ResourceDictionary fails to merge external ResourceDictionariesResourceDictionary 无法合并外部 ResourceDictionaries
【发布时间】:2011-05-18 13:17:30
【问题描述】:

我有一个 ResourceDictionary Generic.xaml,它由一组其他资源字典组成,这是通过合并字典来实现的。如果在我的 Generic.xaml 中没有定义合并字典以外的资源,则在运行时不会应用任何样式。 但是,如果我在 Gerneric.xaml 中定义任何样式,所有样式都将开始正常工作。

所以下面的例子不起作用(这是来自我的 Generic.xaml)

<ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="pack://application:,,,/Resources;component/ResourceDictionaries/CommonControls/CommonControlsResources.xaml"/>
</ResourceDictionary.MergedDictionaries>

<!--<Style TargetType="{x:Type ToolTip}"/>-->

这是工作版本:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Resources;component/ResourceDictionaries/CommonControls/CommonControlsResources.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style TargetType="{x:Type ToolTip}"/>

为什么会这样?

编辑:

为了查明问题,我创建了一个示例解决方案。所以项目的结构如下:

ProjectA WPF Windows 应用程序

ProjectC 类库,包含解决方案文件夹中所有项目共享的样式。

这是项目A:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/ProjectC;component/Themes/ThemeZ.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <!--<Style TargetType="{x:Type ToolTip}"/>-->
</ResourceDictionary>


    <Application x:Class="MergedResourceDictionaryIssue.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/Themes/Generic.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>


    <Window x:Class="MergedResourceDictionaryIssue.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Button/>
        </Grid>
    </Window>

这是项目C:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Styles for all our buttons, let make them all red squares -->
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid Width="50"
                          Height="50"
                          Background="Red" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

如果我取消注释 ResourceDictionary 项目中的代码按预期呈现,否则资源字典将被忽略。

【问题讨论】:

    标签: wpf xaml resourcedictionary


    【解决方案1】:

    据我所知,字典只能包含: - 合并字典 - 或样式

    编辑:他们正在讨论它here这是一个“已知”问题,您使用的技巧是当前的解决方法。

    【讨论】:

    • 你可能误解了我的问题。我刚刚更新了我的问题,我希望现在更清楚问题是什么。
    • 实际上...很抱歉:/您可以在调用 CommonControlsResources.xaml 中的样式的地方放一小段代码吗?您是否已经“最小化”了您的代码?例如,只是在 CommonControlsResources.xaml 中创建一个简单的样式,在一个新的 WPFApplication 中,只有一个控件获得这种简单的样式?
    • @metalcam 我创建了一个示例项目来消除我项目的细节。见上面的代码。
    • 嗯...我无法正确测试,但您是否曾尝试在没有pack://application:..... 的情况下在 1 个项目中做同样的事情?
    • 我的问题不是如何克服这个问题,我自己找到了克服它的方法。问题是为什么会发生这个问题!?我需要在一个单独的项目中有一个 ResourceDictionary。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    相关资源
    最近更新 更多