gpt4 book ai didi

c# - 将 WPF DLL 加载并运行到另一个 WPF exe 中

转载 作者:行者123 更新时间:2023-11-30 22:03:59 25 4
gpt4 key购买 nike

我的主要问题正如我在标题中所述。

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

为什么我要反射(reflection)的是——WPF_APP2先获取最新的WPF_APP1.dll再打开所以不能添加dll的引用。必须仅使用反射。

当我在 cmd 项目中使用上面的 dll 时它没问题。它打开 CMD 窗口,然后以窗口形式启动 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 loading external window(看错了你的帖子,所以这里你也有 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);
}
}
}

希望对您有所帮助!

干杯

斯蒂安

关于c# - 将 WPF DLL 加载并运行到另一个 WPF exe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25579656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com