加载并运行WPF DLL到另一个WPF exe [英] Load and Run WPF DLL into Another WPF exe

查看:168
本文介绍了加载并运行WPF DLL到另一个WPF exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要问题就是我所说的题目。



WPF_APP1 - >在排除App.xaml之后,我创建了这个wpf项目的dll

WPF_APP2 - >正常的WPF exe。需要运行上述WPF_APP1 dll并使用反射打开WPF_APP1 MainWindow窗体。



为什么我在说反思是 -
WPF_APP2 首先得到最新的 WPF_APP1.dll ,然后打开,所以不能添加dll的引用。只能使用反思。



当我在cmd项目中使用上述dll时,它还好。它打开CMD窗口,然后启动 WPF_APP1 MainWindow 作为窗口窗体。



但是现在我需要打开这个窗体,而不是cmd, WPF_APP2



请帮助我。



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;

装配dll = Assembly.LoadFile(loc);

foreach(type type in dll.GetExportedTypes())
{
if(type.Name.Equals(MainWindow))
{
动态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窗口:

 静态类程序
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ThreadProc();
Application.Run(); // 继续跑!
}

private static void ThreadProc()
{
if(System.Windows.Application.Current == null)
new System.Windows。应用();
尝试
{
string assemblyName = string.Format({0} \\\AutoUpdateTesting.dll),新的FileInfo(Assembly.GetExecutingAssembly())位置).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(在assemblyInstance.GetTypes()中键入t)其中(t => String.Equals(t.Name,typeName,StringComparison.OrdinalIgnoreCase))
{
var wnd = assemblyInstance .CreateInstance(t.FullName)为Window;
return wnd;
}
抛出新异常(无法加载外部窗口);
}
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 )
{
启动+ = App_Startup;
}

void App_Startup(object sender,StartupEventArgs e)
{
try
{
string assemblyName = string.Format({ 0} \\AutoUpdateTesting.dll,新的FileInfo(Assembly.GetExecutingAssembly()。Location).DirectoryName);
窗口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);

}
}

私有Window LoadAssembly(String assemblyName,String typeName)
{
try
{
assembly assemblyInstance = Assembly.LoadFrom(assemblyName);
foreach(在assemblyInstance.GetTypes()中键入t)其中(t => String.Equals(t.Name,typeName,StringComparison.OrdinalIgnoreCase))
{
var wnd = assemblyInstance .CreateInstance(t.FullName)为Window;
return wnd;
}
抛出新异常(无法加载外部窗口);
}
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);
}
}
}

希望它有帮助! p>

干杯



Stian


My main issue is as I stated in title.

WPF_APP1 --> I created dll of this wpf project after excluding App.xaml
WPF_APP2 --> Normal WPF exe. which needs to run the above WPF_APP1 dll and open the WPF_APP1 MainWindow form using reflection.

Why I am saying for reflection is - WPF_APP2 first get the latest WPF_APP1.dll then open so can not add the reference of dll. have to use the Reflection only.

When I use the above dll in cmd project its okay. it open the CMD window then launch the WPF_APP1 MainWindow as Window form.

But Now I need to open this window form not in cmd, in WPF_APP2.

Please help me out.

CMD project use the below code to open the 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;
            }
        }

    }

I can not use line -

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

In WPF_APP2 because of AppDomain(found this reason on google). Try other alternative but no luck.

Please have a look and share your knowledge. :)

waiting for your comments and reply.

Thanks

解决方案

Load WPF window from forms application:

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);
        }
    }
}

That does the trick for forms! Remember that you need to add references to the base WPF assemblies(PresentationCore, WindowBase+++)

WPF loading external window (Read your post wrong, so here you have wpf to wpf as well)

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);
        }
    }
}

Hope it helps!

Cheers

Stian

这篇关于加载并运行WPF DLL到另一个WPF exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆