7

我正在尝试将 的 设置DataContext为其MainWindowViewModel in App.OnStartup。我注意到这样做时,MainWindow()构造函数被调用了两次,我看到打开了 2 个窗口。知道是什么导致了这种行为吗?我的代码如下:

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();
    }
}
4

1 回答 1

23

我怀疑还有StartupUri挥之不去的...App.xaml

于 2012-08-24T02:07:08.450 回答