加载并运行WPF DLL到另一个WPF exe

我的主要问题是我在标题中所说的。

WPF_APP1 – >我在排除App.xaml后创建了这个wpf项目的dll
WPF_APP2 – >普通WPF exe。 需要运行上面的WPF_APP1 dll并使用reflection打开WPF_APP1 MainWindow表单。

为什么我说反思是 – WPF_APP2首先获取最新的WPF_APP1.dll然后打开所以无法添加dll的引用。 必须只使用reflection。

当我在cmd项目中使用上面的dll时没关系。 它打开CMD窗口,然后以窗口forms启动WPF_APP1 MainWindow

但是现在我需要在WPF_APP2中打开不在cmd中的窗口

请帮帮我。

CMD项目使用以下代码打开WPF_APP1 MainWindow。

static void Main(string[] args) { Thread t = new Thread(ThreadProc); t.SetApartmentState(ApartmentState.STA); t.IsBackground = true; t.Start(); Console.ReadLine(); } private static void ThreadProc() { string loc = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName + "\\AutoUpdateTesting.dll"; Assembly dll = Assembly.LoadFile(loc); foreach (Type type in dll.GetExportedTypes()) { if (type.Name.Equals("MainWindow")) { dynamic n = null; n = Activator.CreateInstance(type); n.InitializeComponent(); System.Windows.Application apprun = new System.Windows.Application(); apprun.Run(n); break; } } } 

我不能用线 –

  System.Windows.Application apprun = new System.Windows.Application(); 

在WPF_APP2中因为AppDomain(在google上找到了这个原因)。 尝试其他选择,但没有运气。

请查看并分享您的知识。 🙂

等待你的评论和回复。

谢谢

从表单应用程序加载WPF窗口:

 static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ThreadProc(); Application.Run(); // Keep on running! } private static void ThreadProc() { if (System.Windows.Application.Current == null) new System.Windows.Application(); try { string assemblyName = string.Format("{0}\\AutoUpdateTesting.dll", new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName); System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { Window wnd = LoadAssembly(assemblyName, "OtherWindow"); wnd.Show(); })); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(string.Format("Failed to load window from{0} - {1}", "OtherWindow", ex.Message)); throw new Exception(String.Format("Failed to load window from{0} - {1}", "OtherWindow", ex.Message), ex); } } private static Window LoadAssembly(String assemblyName, String typeName) { try { Assembly assemblyInstance = Assembly.LoadFrom(assemblyName); foreach (Type t in assemblyInstance.GetTypes().Where(t => String.Equals(t.Name, typeName, StringComparison.OrdinalIgnoreCase))) { var wnd = assemblyInstance.CreateInstance(t.FullName) as Window; return wnd; } throw new Exception("Unable to load external window"); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(string.Format("Failed to load window from{0}{1}", assemblyName, ex.Message)); throw new Exception(string.Format("Failed to load external window{0}", assemblyName), ex); } } } 

这对表格起作用了! 请记住,您需要添加对基本WPF程序集的引用(PresentationCore,WindowBase +++)

WPF加载外部窗口 (读错了,所以这里你也有wpf到wpf)

 public partial class App : Application { App() { Startup += App_Startup; } void App_Startup(object sender, StartupEventArgs e) { try { string assemblyName = string.Format("{0}\\AutoUpdateTesting.dll", new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName); Window wnd = LoadAssembly(assemblyName, "OtherWindow"); wnd.Show(); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(string.Format("Failed to load window from{0} - {1}", "OtherWindow", ex.Message)); throw new Exception(String.Format("Failed to load window from{0} - {1}", "OtherWindow", ex.Message), ex); } } private Window LoadAssembly(String assemblyName, String typeName) { try { Assembly assemblyInstance = Assembly.LoadFrom(assemblyName); foreach (Type t in assemblyInstance.GetTypes().Where(t => String.Equals(t.Name, typeName, StringComparison.OrdinalIgnoreCase))) { var wnd = assemblyInstance.CreateInstance(t.FullName) as Window; return wnd; } throw new Exception("Unable to load external window"); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(string.Format("Failed to load window from{0}{1}", assemblyName, ex.Message)); throw new Exception(string.Format("Failed to load external window{0}", assemblyName), ex); } } } 

希望能帮助到你!

干杯

了Stian

不是答案,而是一个建议:

我会改变 :

 foreach (Type type in dll.GetExportedTypes()) { if (type.Name.Equals("MainWindow")) { dynamic n = null; n = Activator.CreateInstance(type); n.InitializeComponent(); System.Windows.Application apprun = new System.Windows.Application(); apprun.Run(n); break; } } 

有:

 // replacing the loop, brackets and break with: var main_win = dll.GetExportedTypes().Where(t => t.Name.Equalts("MaindWindow"); dynamic n = null; n = Activator.CreateInstance(type); n.InitializeComponent(); System.Windows.Application apprun = new System.Windows.Application(); apprun.Run(n); 

让linq担心它,让你的代码更容易阅读和维护。