6
我試圖將MainWindow
的DataContext
設置爲App.OnStartup
的ViewModel。我注意到這樣做時,MainWindow()
構造函數被調用兩次,我看到2個窗口打開。任何想法是什麼導致這種行爲?我的代碼如下:調用兩次的MainWindow構造函數
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();
}
}