为什么 Generic.xaml 中的合并字典需要绝对 uri? [英] Why are absolute uri's required for merged dictionaries in Generic.xaml?

查看:59
本文介绍了为什么 Generic.xaml 中的合并字典需要绝对 uri?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个文件 |包含以下内容的 WPF 应用程序的新项目:

Consider a File | New Project of a WPF Application that contains:

  1. 一个名为 CustomControl1 的新自定义控件
  2. 两个名为 Dictionary1 和 Dictionary2 的新资源词典

从 Generic.xaml 中取出生成的样式并将其移动到 Dictionary2.然后像这样将 Dictionary2 合并到 Dictionary1 和 Dictionary1 到 Generic 中:

Take the generated style out of Generic.xaml and move it to Dictionary2. Then merge Dictionary2 into Dictionary1 and Dictionary1 into Generic like this:

<!--Generic.xaml-->
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Themes/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>

<!--Dictionary1.xaml-->
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>

然后,将 CustomControl1 的实例添加到 MainWindow 的网格中.(这部分是重现问题所必需的.项目始终编译良好 - 只有在运行时才会出现问题,并且必须引用字典.)

Then, add an instance of CustomControl1 into MainWindow's grid. (This part is necessary to reproduce the issue. The project always compiles fine - only at runtime does the issue show up, and the dictionaries must be referenced.)

在 Dictionary1.xaml 中,我正在合并同一文件夹中的另一个 dict,因此一个简单的 Source="Dictionary2.xaml" 可以工作.然而在 Generic.xaml 中,我必须使用绝对 URI.如果我将上面的内容更改为 Source="Dictionary1.xaml" 而没有 pack://application 内容,那么当它尝试构造 MainWindow 时,我会收到由 IOException "Cannot locate resource 'dictionary1.xaml'" 引起的 XamlParseException.

In Dictionary1.xaml I am merging in another dict in the same folder, so a simple Source="Dictionary2.xaml" works. Yet in Generic.xaml I must use an absolute URI. If I change the above to be Source="Dictionary1.xaml" without the pack://application stuff then I get a XamlParseException caused by an IOException "Cannot locate resource 'dictionary1.xaml'" when it tries to construct the MainWindow.

我的问题: generic.xaml 在相对 URI 解析方面有什么特别之处,为什么?

推荐答案

对不起,我没有能力写评论,所以我把这个作为答案发布.

Excuse me because I have no ability to write comments so I post this as an answer.

我有同样的情况,一切正常.我不需要将pack://application"放在 Generic.xaml 的路径中.但仅当程序集的输出类型为Windows 应用程序"时.对于 "Class library" 我需要将程序集名称添加到路径 (Source="/ClassLibarayAssemblyName;component/Themes/Dictionary1.xaml") 因为没有它 WPF 引擎会尝试在应用程序的主程序集中查找 Dictionary1.xaml.

I have the same situation and everything works fine for me. I don't need to put "pack://application" in the path in Generic.xaml. But only when the output type of an assembly is "Windows Application". For "Class library" I need to add assembly name to the path (Source="/ClassLibarayAssemblyName;component/Themes/Dictionary1.xaml") becasue without it WPF engine tries to look for Dictionary1.xaml in application's main assembly.

两种情况下的目标框架都是.NET Framework 4 Client Profile"

Target framework in both cases is ".NET Framework 4 Client Profile"

这篇关于为什么 Generic.xaml 中的合并字典需要绝对 uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆