El constructor de MainWindow es llamado dos veces

Estoy tratando de configurar el DataContext de los MainWindow to its ViewModel in App.OnStartup. I noticed when doing that, MainWindow() constructor is getting called twice and I see 2 windows opened. Any idea whats causing this behavior ? My code is as follows:

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

preguntado el 24 de agosto de 12 a las 03:08

1 Respuestas

Todavía existe el StartupUri lingering in App.xaml i suspect...

Respondido 24 ago 12, 03:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.