Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MainWindow constructor getting called twice

Tags:

c#

wpf

xaml

I am trying to set the DataContext of the 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();
    }
}
like image 991
Frank Q. Avatar asked Aug 24 '12 02:08

Frank Q.


1 Answers

There is still the StartupUri lingering in App.xaml i suspect...

like image 135
H.B. Avatar answered Oct 20 '22 05:10

H.B.