Multiple WPF applications in the same AppDomain

前端 未结 3 993
时光取名叫无心
时光取名叫无心 2021-01-18 03:57

I got following setup:

WPF_Application.exe

and

a DLL that contains a WinForms Window and an WPF Window.

The \'WPF_Application.exe\' calls th

相关标签:
3条回答
  • 2021-01-18 04:08

    Make sure you only have one Application defined in XAML.

    Visual studio creates an App.xaml when making a new WPF solution, if you have created a new .xaml file with the <Application></Application> tag, this will cause this error and the solution will build but fail at runtime.

    Delete the duplicate application and use Window or UserControl to have multiple windows.

    0 讨论(0)
  • 2021-01-18 04:23

    System.Windows.Application is a singleton: its constructor must only be invoked once (including App.xaml de-xamlization), or exception is thrown.

    I'm affraid there is not much you can do about it, except maybe check if Application.Current is set before starting your second application, and somewhat use that instance to load it.

    Or you can create another AppDomain, and use it to launch the second application.

    0 讨论(0)
  • 2021-01-18 04:25

    I fixed it by asigning the Application of the wpf window that is called to the Application.Current Window:

      if (Application.Current == null)
      {
           MyApplication = new Application
           {
              ShutdownMode = ShutdownMode.OnExplicitShutdown
           };
       }
       else
           MyApplication = Application.Current;
    
    0 讨论(0)
提交回复
热议问题