C# MainWindow构造函数被调用两次

C# MainWindow构造函数被调用两次,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试将main窗口的DataContext设置为App.onstart中的ViewModel。我注意到在执行此操作时,MainWindow()constructor被调用了两次,我看到打开了两个窗口。知道是什么导致了这种行为吗?我的代码如下: public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStart

我正在尝试将
main窗口的
DataContext
设置为
App.onstart
中的ViewModel。我注意到在执行此操作时,
MainWindow()
constructor被调用了两次,我看到打开了两个窗口。知道是什么导致了这种行为吗?我的代码如下:

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();
    }
}
我怀疑,
App.xaml