设置 .xaml 组件的资源 URI [英] Set resource URI of .xaml component

查看:34
本文介绍了设置 .xaml 组件的资源 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" ?

其中:

解决方案

注意,

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

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

这会导致 TargetInvocationException,即

<块引用>

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

如下:

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

此致谢:

I have a .xaml UserControl named MyUserControl.xaml and I want to set its resource URI.

Per default, WPF generates a URI that includes a resource name, which is equal to the resource it belongs to such as

"/MyNamespace;component/myusercontrol.xaml"

for the .xaml named MyUserControl.xaml

How can I have a UserControl MyUserControl.xaml and make WPF generate an individual resource identifies such as

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

The reason why I want to do that is described here.

In the image below you can see the resource identifier I am talking about:

and therein:

解决方案

Note, that that question is the origin of this question and might help to understand its background.

After a week suffering and laboring with this issue, I finally found both the reason for the problem and its solution.

The problem lies within the auto-generated *.g.i.cs file, which is called by the InitializeComponent() method of a UserControl, as seen by the following:

This file generates a string (a Resource Locator) that expresses the path to that xaml-component, as seen by the following:

Now, if you have multiple versions of the same assembly and both versions include the same xaml-file, WPF does not know what xaml-file to instantiate, because the Resource Locator only references the name of the assembly but not its version.

This results in a TargetInvocationException, saying that

{"The component 'MyNamespace.MyUserControl' does not have a resource identified by the URI '/MyAssembly;comoponent/myusercontrol.xaml'"}

as follows:

The simple (but most definitely not obvious) solution for this is to add the version of the assembly to this Resource Locator. This can be achieved by modifying the build-file of the project by adding the <AssemblyVersion>-tag as follows:

Credits for this go to:

这篇关于设置 .xaml 组件的资源 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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