2012-08-24 112 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我懷疑...

相關問題