2012-08-24 12 views
6

Sto provando a impostare DataContext di MainWindow nel suo ViewModel in App.OnStartup. Ho notato quando lo faccio, il costruttore MainWindow() viene chiamato due volte e vedo 2 finestre aperte. Qualche idea che cosa sta causando questo comportamento? Il mio codice è il seguente:Costruttore MainWindow chiamato per due volte

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

risposta

22

C'è ancora il StartupUri persistente in App.xaml ritengo sospetto ...

Problemi correlati