【发布时间】: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 中标准控件的自定义样式的微妙之处,并将发布关于为什么会发生这种情况的答案。这个错误是我的错(和往常一样),但识别起来可能非常棘手。