WPF 控件抛出“由 URI 标识的资源丢失"异常 [英] WPF control throwing 'resource identified by the URI missing' exception

查看:35
本文介绍了WPF 控件抛出“由 URI 标识的资源丢失"异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加载插件并尝试创建XYZ"控件时,应用程序抛出以下异常:

On loading the plugin and trying to create 'XYZ' control, the application throws the following exception:

"组件 'XYZ' 没有由 URI 标识的资源'/ThePluginAssembly;component/XYZ.xaml'"在 InitializeComponent() 方法上UserControls 构造函数.

"The component 'XYZ' does not have a resource identified by the URI '/ThePluginAssembly;component/XYZ.xaml'" on the InitializeComponent() method in the UserControls constructor.

关键点是:

  1. 用户控件位于插件程序集中

  1. The user control is located in the plugin assembly

我正在尝试从插件程序集中创建用户控件

I am trying to create the usercontrol from inside the plugin assembly

插件与主应用程序位于同一目录

The plugins are located in the same directory as the main application

用户控件只有在通过 XAML 创建时才会出现问题.我在同一个程序集中有几个其他用户控件,但我使用代码实例化了它们.我仅在尝试在 XAML 中创建 UserControl 时收到错误消息.

The user controls only have problems when they are created through XAML. I have a couple of other usercontrols in the same assembly but I instantiate these using code. I only receive the error message when I attempt to create a UserControl in XAML.

在做一些谷歌时,我意识到当我的插件的两个实例加载到应用程序中时会发生这种情况.当我从其中一个文件夹中删除我的插件(我允许从两个位置加载该插件)时,此异常停止重复发生.

On doing some google, i realized that this happens when two instances of my plugin are loaded in the application. When i removed my plugin from one of the folders ( I allow this plugin to be loaded from two locations) this exception stopped recurring.

我的问题:

1) WPF 尝试解析 URI 以加载我的控件的原因是什么?

1) What is the reason behind WPF trying to resolve a URI to load my control?

2) 有没有办法让我的插件的两个实例加载到应用程序中并以某种方式摆脱这个异常?或者以某种方式为每个实例创建唯一的 URI(如果此异常是由冲突的 URI 引起的).

2) Isn't there a way by which i could have two instances of my plugin loaded in the application and somehow get rid of this exception? or some way to create a unique URI for each instances (if this exception is caused by a conflicting URI).

任何评论或参考都会有所帮助.

Any comment or reference would be of help.

感谢您的关注.

与 Phil 发布的问题相同:如何强制 WPF 使用使用程序集强名称的资源 URI?啊!

Same problem as posted by Phil : How to force WPF to use resource URIs that use assembly strong name? Argh!

推荐答案

执行此操作的唯一方法是在 URI 中包含版本信息,以便 XAML 加载程序可以区分正确的类型.这个 MSDN 文章解释了 Pack URI 格式,版本部分有这个说明:

The only way to do this would be to include the version information in your URI, so the XAML loader can distinguish the correct type. This MSDN article explains the Pack URI format, and the Version portion has this description:

;Version [可选]:包含资源文件的引用程序集的版本.当加载了两个或多个具有相同短名称的引用程序集时使用.

;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.

所以你想使用以下之一:

So you'd want to use one of the following:

/ThePluginAssembly;v1.0.0.0;component/XYZ.xaml
/ThePluginAssembly;v1.1.0.0;component/XYZ.xaml

这篇关于WPF 控件抛出“由 URI 标识的资源丢失"异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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