Application.LoadComponent来自未引用的程序集加载时失败 [英] Application.LoadComponent fails when loading from an unreferenced assembly
问题描述
我有一个需要能够加载predefined 用户控件
s为基础在一个开放的WPF应用程序。我使用 Application.LoadComponent(URI)
要做到这一点,但它现在抛出一个 IOException异常
与无法找到资源......
。
I have a WPF application that needs to be able to load predefined UserControl
s based upon a Uri. I'm using Application.LoadComponent(Uri)
to do this, but it's now throwing an IOException
with Cannot locate resource...
.
我原有的建筑看起来像这一点,并发挥预期的:
My original architecture looked like this and worked as expected:
Application Assembly/
Main Window.xaml
Custom Control.xaml
Dynamically Loaded Control.xaml
Second Assembly/
Class that executes Application.LoadComponent("Dynamically Loaded Control.xaml")
不过,我需要能够移动自定义控制
和动态加载控制
到另一个程序,是不是通过应用程序组装
(直接引用,因为这第二个组件将引用应用程序组装
,所以引用是圆形的,也有可能也未在编译时已知为核心应用附加组件,所以这是不幸的一个明确的要求)。
However, I need to be able to move Custom Control
and Dynamically Loaded Control
into another assembly that is not directly referenced by Application Assembly
(as this second assembly will reference Application Assembly
, so the reference would be circular; there may also be additional assemblies that are not known at compile time for the core application, so this is unfortunately a definite requirement).
我已经重新设计它,事情是这样的,现在:
I've rearchitected it and things look like this now:
Application Assembly/
Main Window.xaml
Second Assembly/
Class that executes Application.LoadComponent("Dynamically Loaded Control.xaml")
Third Assembly
Custom Control.xaml
Dynamically Loaded Control.xaml
我可以动态地实例自定义Control.xaml
(通过自定义的 DataTemplateSelector
的方式)后装载第三组装
按文件名,这按预期工作。然而,类第二届大会
现在抛出上述异常时,获得通过相同路径动态添加控件。 (在大会)的路径没有改变,我曾经尝试都:
I can dynamically instantiate Custom Control.xaml
(by way of a custom DataTemplateSelector
) after loading Third Assembly
by file name and this works as expected. However, the class in Second Assembly
now throws the above exception when getting passed the same path to the dynamically added control. The paths (within the assembly) have not changed, and I have tried both:
Application.LoadComponent("Dynamically Loaded Control.xaml");
和
Application.LoadComponent("Third Assembly;Dynamically Loaded Control.xaml");
和不幸都产生相同的结果。我能想到的唯一的问题是,自定义和动态控制,现在是在被明确载入,而不是通过引用程序集,但我不能确定为什么会有所作为。
And unfortunately both produce the same result. The only issue I can think of is the fact that the custom and dynamic controls are now in an assembly that is loaded explicitly rather than via reference, but I'm not certain why that would make a difference.
推荐答案
在我决定后的问题,我设法找到一个小时的解决方案
Once I decided to post the question, I managed to find the solution in under an hour.
为了做到这一点,我不得不使用的形式:
In order to accomplish this, I had to use the form:
Application.LoadComponent(
"/Third Assembly;component/Dynamically Loaded Control.xaml");
在组件
部分是很难codeD;这不是在我的组件的任何地方,但它似乎是必需的,以得到加载的组件。如果任何人都可以提供答案,包括这些数据,并提供了有关为什么是这样的情况下,更详尽的解释,我会很乐意接受它!
The component
portion is hardcoded; that is not in my assembly anywhere, but it appears to be required for the component to get loaded. If anyone can provide an answer that includes this data and provides a more thorough explanation about why this is the case, I'll gladly accept it!
感谢这篇博客帮助我出来。
这篇关于Application.LoadComponent来自未引用的程序集加载时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!