Home > Software engineering >  Get a list of running WPF Windows on a WinForm application
Get a list of running WPF Windows on a WinForm application

Time:07-25

I've implemented some new WPF stuff in a old WinForm app project and I used to run it as the following:

    WpfWindow win = new WpfWindow(); // is a subclass of System.Windows.Window
    System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(win);
    WindowInteropHelper helper = new WindowInteropHelper(win);
    helper.Owner = this.Handle;

    win.Show();

now the question is, how can I get a list of opened Windows? In a native WPF project I simply do:

var windows = Application.Current.Windows

but I did not find any way in a form project.

CodePudding user response:

Before you show the first Window, for example in startup of your application, create an instance of System.Windows.Application (and forget it).

var app = new System.Windows.Application();

Later, in your WinForms application, you can easily find all WPF windows:

var windows = System.Windows.Application.Current.Windows

Note:

  • Do this only once, for example in startup of the application. Look at source code of the application class.

  • It's pretty similar to the way that a WPF app does it:

    public partial class App : System.Windows.Application 
    {
        ...
        ...
        ...         
        public static void Main() 
        {
            WpfApp1.App app = new WpfApp1.App();
            app.InitializeComponent();
            app.Run();
        }
    }
    
  • Related