tags:

views:

35

answers:

1

I have a wpf application and I created a logon window which is used to build the application's connection string. I am having issues closing the first dialog and spinning open the MainWindow behind it. I think a close event is bubbling out of the logon dialog and getting stuck in the MainWindow because as soon as I create the MainWindow object in the codebehind and call Show() it just moves right past my Startup event handler and into my constructor then the onClosing handlers of the MainWindow without ever showing the window itself. The app.xaml has the ShutdownMode="OnMainWindowClose" specified.

private void Application_Startup(object sender, StartupEventArgs e)
    {
        try
        {
            Chooser thechooser = new Chooser();
            thechooser.ShowDialog();
        }
        catch (Exception ex)
        {

        }
        //initialize datalayer
        dataLayer = new Mxxx41.DAL(this.CurrentConnectionString);
        MainWindow appmainwindow = new MainWindow();
        Application.Current.MainWindow = appmainwindow;
        appmainwindow.Activate();
        appmainwindow.Show();
}

private void LogInButton_Click(object sender, RoutedEventArgs e)
    {
        //get ip from listbox selection
        XmlElement currentelement = (XmlElement)Listbox.SelectedItem;

        string ip = ((string)currentelement.Attributes["IP"].Value);
        string instancename = string.Empty;
        if (!((string)currentelement.Attributes["InstanceName"].Value == string.Empty))
        {
            instancename = ((string)currentelement.Attributes["InstanceName"].Value);
        }
        //ping that IP
        Boolean pingresult = ping.PingHost(ip);
        Boolean sqlresult = false;
        if (pingresult)
        {
            if (!(String.IsNullOrEmpty("instancename")))
            {
                ip = string.Format("{0}\\{1}", ip, instancename); 
            }

            //build connection string with that IP
            string connstr = BuildConnStr(ip);

            //create datalayer
            Mxxx41.DAL datalayer = new Mxxx41.DAL(connstr);
            //validate credentials
            DataSet data = datalayer.getDataSet("login_checkcredentials", CommandType.StoredProcedure, datalayer.CreateParameter("@username", SqlDbType.VarChar, this.UsernameTextbox.Text), datalayer.CreateParameter("@password", SqlDbType.VarChar, this.PasswordTextbox.Text));
            if (data.Tables[0].Rows.Count > 0)
            {
                sqlresult = true;

                //log in user
                //build new user code omitted for brevity


                App myAppReference = ((App)Application.Current);
                myAppReference.CurrentUser = thisuser;
                myAppReference.CurrentConnectionString = connstr;
                //close window
                this.Close();  //this is the close event I think is causing issues.
            }

        }
        else 
        { 
            ErrorLabel.Content = string.Format("{0}{1}", "could not ping selected Host :", ip); 
        }

        //return true


    }

public MainWindow(){
        this.InitializeComponent();

        this.SideBarExpander.IsExpanded = true;

        this.Loaded += onLoaded;
        this.Closed += onClosed;
        this.Closing += onClosing;

        try
        {
            //this.DataLayer = ((Mxxx41.DAL)MyDemoApp.App.Current.Properties["DataLayer"]);
            App myAppReference = ((App)Application.Current);
            this.DataLayer = myAppReference.GetDataLayer();
        }
        catch //catch everything for the moment
        {
            this.DataBaseConnectionError = true;
        }
        ExceptionList = new List<Error>();
    }

Can someone help me out with this behavior?

+1  A: 

The problem is probably with ShutdownMode="OnMainWindowClose". Wpf considers the first window opened to be the "main window". In your case, wpf sees your logon window as the main window and exits your application when it closes.

Try changing the shutdown mode to OnLastWindowClose or OnExplicitShutdown.

From MSDN:

OnMainWindowClose: An application shuts down when either the main window closes, or Shutdown is called.
OnExplicitShutdown: An application shuts down only when Shutdown is called.

Zach Johnson
Thanks Zach. This was correct. I did not understand that my dialog window stole the MainWindow reference even though i reset the MainWindow object a few lines below.
TWood
@TWood: You're welcome. I think the confusing thing is probably that `OnMainWindowClose` doesn't mean when the `MainWindow` window closes, but when the first window opened closes.
Zach Johnson