【问题标题】:Executing WPF Assembly from Memory从内存中执行 WPF 程序集
【发布时间】:2011-05-01 15:25:48
【问题描述】:

这种方法here(如下所示)适用于大多数应用程序。

FileStream fs = new FileStream(filePath, FileMode.Open); // read the bytes from the application EXE file
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
Assembly a = Assembly.Load(bin); // load the bytes into Assembly
MethodInfo method = a.EntryPoint; // search for the Entry Point
if (method != null) 
{
     // create an instance of the Startup form Main method
     object o = a.CreateInstance(method.Name);
     // invoke the application starting point
     method.Invoke(o, null); //EXCEPTION THROWN HERE
}

但是,当我尝试使用它来启动 WPF 应用程序时,抛出了异常:

Exception has been thrown by the target of an invocation.

内部异常是 System.IO.IOException 类型:

Cannot locate resource 'mainwindow.xaml'.

注意:应用程序可以正常运行。出于测试目的,编译了一个空的 wpf 应用程序。

【问题讨论】:

  • 您是否重命名了您的用户控件文件或将您的启动位置文件移动到不同的位置(在子文件夹中)?

标签: wpf reflection .net-assembly


【解决方案1】:

当您将启动 xaml 文件移动到其他位置或重命名但忘记更新 App.xaml 文件时,通常会出现此错误。如果您重命名它,只需像这样更新您的 App.xaml 文件 - StartupUri="mainwindow.xaml" 或者如果您将它移动到像这样的不同子文件夹更新 - StartupUri="FolderName/mainwindow.xaml"

【讨论】:

  • 如果你重命名它,请确保文件后面的代码也被重命名。
  • 这不是问题。应用程序正常运行。
  • 您确定您没有“正常”启动旧版本的 WPF 可执行文件吗?
  • 是的,我确定。请自己尝试一下,看看问题.. 需要 2 分钟来制作一个空的 wpf 应用程序,然后制作一个控制台或任何应用程序来运行上述代码
猜你喜欢
  • 2011-04-18
  • 2012-06-20
  • 2012-10-01
  • 1970-01-01
  • 2017-05-29
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多