How to create two windows in WPF?

In WPF, you can create two windows using the following steps:

  1. Create a new WPF application project.
  2. Add a new WPF window to the project. Right-click on the project folder, select “Add”->”New Item”->”WPF Window”.
  3. Design the UI interface in the XAML file of the window.
  4. In the entry point of the application (typically the App.xaml.cs file), create two instances of windows and launch the main window.

Here is an example code:

The code file for the main window in the application is MainWindow.xaml.cs.

using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

The code-behind file for SecondaryWindow.xaml is SecondaryWindow.xaml.cs.

using System.Windows;

namespace WpfApp1
{
    public partial class SecondaryWindow : Window
    {
        public SecondaryWindow()
        {
            InitializeComponent();
        }
    }
}

Application.xaml.cs:

using System.Windows;

namespace WpfApp1
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            MainWindow mainWindow = new MainWindow();
            SecondaryWindow secondaryWindow = new SecondaryWindow();

            mainWindow.Show();
            secondaryWindow.Show();
        }
    }
}

This creates a WPF application with two windows – a main window called MainWindow and a secondary window called SecondaryWindow. Both windows will be displayed simultaneously when the application is launched.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds