2012-08-24 37 views
6

MainWindowDataContextをViewModelのApp.OnStartupに設定しようとしています。私はそれを行うとき気づいたMainWindow()コンストラクタが2回呼び出されていると私は2つのウィンドウが開いて参照してください。どのようなアイデアは、この動作の原因は何ですか?次のように私のコードは次のとおりです。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(); 
    } 
} 

答えて

22

は私が疑われるApp.xamlStartupUri余韻が残っている...

関連する問題