2012-08-24 97 views
6

我试图将MainWindowDataContext设置为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(); 
    } 
} 

回答

22

目前仍是StartupUri萦绕在App.xaml我怀疑...

相关问题