开发者

Application.LoadComponent fails when loading from an unreferenced assembly

I have a WPF application that needs to be able to load predefined UserControls 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

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");

and

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 woul开发者_StackOverflow中文版d make a difference.

How can I get Application.LoadComponent to properly load a component that is compiled into an assembly that I load explicitly at runtime?


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");

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!

Credit to this blog post for helping me out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜