从内存执行WPF程序集 [英] Executing WPF Assembly from Memory

查看:57
本文介绍了从内存执行WPF程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在不久前的stackoverflow论坛上被问到的,但是没有得到回答.有人有解决方案吗?

如果asm变量是MemoryStream并包含.Net程序集,则通常应按以下方式运行它:

This was asked on the stackoverflow forums a while back, but was unanswered. Does anyone have a solution?

If the asm variable is a MemoryStream and contains a .Net assembly, you would normally run it like this:

var asm   = Assembly.Load(ms.ToArray());
var entry = asm.EntryPoint;
var inst  = asm.CreateInstance(entry.Name);
entry.Invoke(inst, null);



这在控制台应用程序和Windows窗体应用程序上效果很好,但是WPF应用程序会抛出异常:



This works well on console applications and windows forms applications, however, WPF applications throw an exception:

Exception has been thrown by the target of an invocation.



除了类型为System.IO.IOException的内部异常:



With the inner exception of type System.IO.IOException:

Cannot locate resource ''mainwindow.xaml''.

推荐答案

原则上,此代码看起来正确;问题出在装配件中.

可疑的一行是"CreateInstance".它将创建某种类型的实例,该实例的名称可能与入口点的名称不同.您可能需要看一下我使用插件并找到自定义入口点的非常强大的设计.我在这里描述其骨架:
创建使用可重载插件的WPF应用程序... [ ^ ],
AppDomain拒绝加载程序集 [
为了找出问题所在,创建要加载到磁盘上的程序集,并尝试直接在其他项目中引用该程序集,而不是调用其入口点的实现;这样,将在调试器中更明确地显示出错误所在.

使用XAML将WPF程序集放在库中并不是一件容易的事.如果您要引用的程序集试图创建和运行WPF应用程序,则该程序集应包含这样的入口点:

This code looks correct, in principle; the problem is in the assembly being loaded.

One line which is a suspect is "CreateInstance". It would create instance of some type, which may have not the same name as the entry point. You may want to look my very robust design of working with plug-ins and finding custom entry point instead. I describe its skeleton here:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^].

Please ignore everything related to re-loadable stuff and Application Domain. You probably only need to load your plug-in assembly only once, so this is not a problem at all.

The exception "Exception has been thrown by the target of an invocation" is very typical for invocation of something which will through exception.

To find out what''s wrong, create the assembly to be loaded on disc and try to reference this assembly in the some other project directly and than call its implementation of entry point; in this way it will be shown what''s wrong more explicitly under debugger.

Putting a WPF assembly using XAML in a library is not so trivial task. If the assembly you''re referencing it trying to create and run the WPF application, it should contain the entry point like this:

//assuming class MyApplication : Application {}:
MyApplication application = new MyApplication(/*...*/);
//assuming MyMainWindow : Window {/*...*/}
application.MainWindow = new MyMainWindow();
application.MainWindow.Icon = //...
application.MainWindow.Show();
application.Run();



那是你在做什么吗?我最近开发了一个系统,其中WPF应用程序在单独的程序集中编译的类库中运行;很完美.

-SA



Is that what you are doing? I recently developed a system where WPF application is run in the class library compiled in a separate assembly; it worked out perfectly.

—SA


如果mainwindow.xaml包含位于单独程序集中的任何资源,则可能会发生这种情况.
Maybe this could happen if the the mainwindow.xaml contain any resources that are located in a separate assembly.


这篇关于从内存执行WPF程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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