设置.xaml组件

时间:2016-01-28 07:03:08

标签: wpf xaml resources uri

我有一个名为MyUserControl.xaml的.xaml UserControl,我想设置它的资源URI。

默认情况下,WPF生成一个包含资源名称的URI,该名称等于它所属的资源,例如

"/MyNamespace;component/myusercontrol.xaml"

表示名为MyUserControl.xaml的.xaml

如何拥有UserControl MyUserControl.xaml并使WPF生成单个资源标识,例如

"/MyNamespace;component/myusercontrol_A.xaml""/MyNamespace;component/myusercontrol_B.xaml"

我想要这样做的原因是here

在下图中,您可以看到我正在谈论的资源标识符:

enter image description here

其中:

enter image description here

1 个答案:

答案 0 :(得分:2)

请注意,that question是此问题的来源,可能有助于了解其背景。

经过一周的痛苦和苦苦挣扎,我终于找到了问题的原因及其解决方案。

问题在于自动生成的*.g.i.cs文件,该文件由InitializeComponent()的{​​{1}}方法调用,如下所示:

enter image description here

此文件生成一个字符串(资源定位器),表示该xaml组件的路径,如下所示:

enter image description here

现在,如果您有同一程序集的多个版本并且两个版本都包含相同的xaml文件,那么 WPF 不知道要实例化的xaml文件,因为资源定位器仅引用程序集的名称,但不引用其版本。

这导致UserControl,说

  

{“组件'MyNamespace.MyUserControl'没有由URI标识的资源'/ MyAssembly; comoponent/myusercontrol.xaml'”}

如下:

enter image description here

这个简单(但绝不是很明显)的解决方案是将程序集的版本添加到此资源定位器。这可以通过添加TargetInvocationException - 标记修改项目的构建文件来实现,如下所示:

enter image description here

对此的信誉转到:

相关问题