如何从内存执行WPF程序集?

发布于 2024-09-25 14:06:07 字数 1015 浏览 3 评论 0原文

如果 ms 变量是 MemoryStream 并且包含 .Net 程序集,通常会像这样运行它:

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

这在控制台应用程序和 Windows 窗体应用程序上运行良好,但是,WPF应用程序抛出异常:

Exception has been thrown by the target of an invocation.

使用 System.IO.IOException 类型的内部异常:

Cannot locate resource 'mainwindow.xaml'.

堆栈跟踪确实很大,但从一开始就猜测,从内存加载时它无法找到资源:

at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream()
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
at System.Windows.Application.DoStartup()
at System.Windows.Application.<.ctor>b__1(Object unused)
[...]

怎么可能我修好这个吗?

If the ms 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);

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.

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

Cannot locate resource 'mainwindow.xaml'.

The stacktrace is really big, but guessing from the start, it can't locate the resources when loaded from memory:

at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream()
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
at System.Windows.Application.DoStartup()
at System.Windows.Application.<.ctor>b__1(Object unused)
[...]

How could I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-10-02 14:06:08

当您从 MemoryStream 动态加载程序集时,其工作目录将是您自己的程序集的工作目录。该目录不太可能包含程序集引用的 XAML 标记文件。

尝试将 Environment.CurrentDirectory 设置为包含必要的 XAML 的新目录,至少在程序集加载和类实例化期间是如此。

When you load an assembly dynamically from a MemoryStream, its working directory will be that of your own assembly. That directory is unlikely to contain the XAML markup files referred to by the assembly.

Try setting your Environment.CurrentDirectory to a new directory containing the necessary XAML, at least for the duration of the assembly loading and class instantiation.

溇涏 2024-10-02 14:06:08

正如 SAKryukov 在代码项目 此处的建议,我将 WPF 应用程序制作成一个带有自定义入口点的库,然后用我的第二个应用程序调用该入口点。问题似乎在于 App.xaml 实现 starturi 的方式

As suggested by SAKryukov on codeproject here, I made the WPF app into a library with a custom entry point which I then invoke with my 2nd application. The problem seems to lie with the way that App.xaml implements the starturi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文