2011-05-01 67 views
2

如下所示,此方法here对大多数应用程序运行良好。从内存中执行WPF程序集

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应用程序。

+0

您已重新命名用户控件文件或移动您的启动位置的文件,以不同的位置(在子文件夹)? – 2011-05-01 15:34:20

回答

0

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

+0

如果您重命名它,请确保文件后面的代码也被重命名。 – 2011-05-01 15:44:45

+0

那不是那个问题。该应用程序正常运行良好。 – 2011-05-01 16:13:31

+0

你确定你没有开始“正常”的WPF可执行文件的旧版本吗? – BoltClock 2011-05-01 16:19:17