【问题标题】:Why does this XAML merged resource configuration fail at runtime?为什么此 XAML 合并资源配置在运行时失败?
【发布时间】:2014-09-13 20:16:49
【问题描述】:

我有以下 App.xaml:

<Application
    x:Class="Genisyss.V2.Client.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Genisyss.V2.Client"
    ShutdownMode="OnExplicitShutdown">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary
                    Source="Resources/ShellResources.xaml" />
                <ResourceDictionary>
                    <local:AppBootstrapper
                        x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

ShellResources.xaml 如下所示:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    x:Class="Genisyss.V2.Client.Resources.ShellResources"
    x:ClassModifier="public">

    <ResourceDictionary.MergedDictionaries>

        <!-- EXTERNAL RESOURCES -->
        <ResourceDictionary
            Source="/Teton.Wpf;component/Themes/Generic.xaml" />
        <ResourceDictionary
            Source="Images.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <!-- controls and templates defined, etc -->

</ResourceDictionary>

以这种方式配置,程序在运行时因“找不到资源”或“xaml 解析异常”而失败。

如果我将 App.xaml 更改为还包括外部资源,如下所示:

<Application
    x:Class="Genisyss.V2.Client.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Genisyss.V2.Client"
    ShutdownMode="OnExplicitShutdown">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>

                <!-- NOTE ADDITION OF EXTERNAL RESOURCES -->
                <ResourceDictionary
                    Source="/Teton.Wpf;component/Themes/Generic.xaml" />

                <ResourceDictionary
                    Source="Resources/ShellResources.xaml" />
                <ResourceDictionary>
                    <local:AppBootstrapper
                        x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

现在我的程序在运行时找到资源并且运行没有错误。什么给了这个?我认为合并资源的目的是 ACTUALLY TO MERGE THE RESOURCES,这样您就不需要在两个地方声明它们了。

编辑

将 ShellResources.xaml 中的 Source 属性更改为绝对包 uri:

pack://application:,,,/Teton.Wpf;component/Themes/Generic.xaml

但这并没有什么不同。

【问题讨论】:

  • 请注意,但这可能与应用程序的搜索策略不同有关。关于 Source 属性,尝试使用绝对 URI 引用它们:msdn.microsoft.com/en-us/library/vstudio/…
  • 感谢您的建议。我在 ShellResources.xaml 中将其更改为 pack://application:,,,/Teton.Wpf;component/Themes/Generic.xaml... 但没有区别。
  • 我刚刚在这里尝试过类似的场景,但没关系,不过我没有引导程序类型。我们需要知道是什么导致了错误,注释这些行并试图找出谁是罪魁祸首。
  • 我怀疑这与 App.xaml 中缺少 StartupUri 有关。正如您所指出的,我使用的是引导程序。但是,我确实验证了 App.xaml.cs 中正在调用 InitializeComponent()。错误发生在来自 Teton.Wpf dll 的自定义控件之一中,而不是本地控件之一中,如果它提供任何提示或 a-ha 类型的警钟的话。
  • @Aybe,感谢您的 cmets。我发现了 Generic.xaml 中标准控件的自定义样式的微妙之处,并将发布关于为什么会发生这种情况的答案。这个错误是我的错(和往常一样),但识别起来可能非常棘手。

标签: c# wpf xaml


【解决方案1】:

这个错误的出现是因为需要在资源文件中定义事物的顺序。我有一个 WPF 控件项目,它定义了一些自定义控件并覆盖了几个内置控件的默认样式。

长话短说,我的资源在Generic.xaml 文件中被进一步定义,并且它们被进一步访问 - 这是一个禁忌。资源显然只解析一次,以严格的自上而下的方式。我的印象是它们像普通的 c# 源代码一样以两次或多次传递的方式进行解析。

为避免这种情况发生,以下是在 WPF 控件项目中布置 Generic.xaml 的建议方法:

<!-- Shared resources like brushes and colors -->
<!-- DEFAULT control styles, e.g. <Style TargetType="{x:Type TextBox}"... -->
<!-- Control styles for custom controls that are defined in your project -->

感谢@Aybe 推动我深入研究。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多