2012-08-24 15 views
6

Ich versuche, die DataContext von MainWindow zu seinem ViewModel in App.OnStartup zu setzen. Ich habe bemerkt, wenn das gemacht wird, MainWindow() Konstruktor wird zweimal aufgerufen und ich sehe 2 Fenster geöffnet. Irgendeine Idee, was dieses Verhalten verursacht? Mein Code ist wie folgt:MainWindow-Konstruktor wird zweimal aufgerufen

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

Antwort

22

Es ist immer noch das StartupUri Verweilen in App.xaml Ich vermute, ...

Verwandte Themen