Der MainWindow-Konstruktor wird zweimal aufgerufen

Ich versuche das einzustellenDataContext desMainWindow zu seinem ViewModel inApp.OnStartup. Mir ist dabei aufgefallen,MainWindow() Der Konstruktor wird zweimal aufgerufen und es werden 2 Fenster geöffnet. Irgendeine Idee, was dieses Verhalten verursacht? Mein Code lautet wie folgt:

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

        MainWindow mainWindow = new MainWindow();

        // Create the ViewModel to which the main window binds.
        MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();

        // Register handle such that when the mainWindowViewModel asks to be closed, close the window.
        mainWindowViewModel.RequestClose += delegate(System.Object o, System.EventArgs eventArgs)
        {
            mainWindow.Close();
        };


        mainWindow.DataContext = mainWindowViewModel;

        mainWindow.Show();
    }
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage