wpf

Doosan published on
1 min, 108 words

Categories: post

Sometimes two windows appear when running a WPF application in Visual Studio.

App.xaml

<Application x:Class="Trading.WPFClient.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Trading.WPFClient"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

App.xaml.cs

 public partial class App : Application
 {
     protected override void OnStartup(StartupEventArgs e)
     {
         MainWindow = new MainWindow()
         {
             DataContext = new MainViewModel()
         };
         MainWindow.Show();
         base.OnStartup(e);
     }
 }

StartupUri="MainWindow.xaml" automatically shows that UI when the application starts. But OnStartup also creates new MainWindow() and calls Show(), so two windows are created.

Remove one of them. Usually, choose either StartupUri, or remove StartupUri and create the window directly in OnStartup. Do not use both.

Problem With WPF C# App Spawning Two Main Windows

Ref