2010-09-17 10 views
5

Tengo una aplicación wpf y he creado una ventana de inicio de sesión que se utiliza para construir la cadena de conexión de la aplicación. Tengo problemas para cerrar el primer cuadro de diálogo y abrir la MainWindow detrás de él. Creo que un evento cercano está surgiendo del diálogo de inicio de sesión y atascado en MainWindow porque tan pronto como creo el objeto MainWindow en el código subyacente y llamo a Show(), simplemente pasa al controlador de eventos de inicio y al constructor, luego onClosing manejadores de MainWindow sin mostrar la ventana en sí. El app.xaml tiene el ShutdownMode = "OnMainWindowClose" especificado.wpf- Application_Startup - ventana de diálogo de carga ENTONCES mainwindow

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

¿Alguien me puede ayudar con este comportamiento?

Respuesta

4

El problema es probablemente con ShutdownMode="OnMainWindowClose". Wpf considera que la primera ventana abierta es la "ventana principal". En su caso, wpf ve su ventana de inicio de sesión como la ventana principal y sale de su aplicación cuando se cierra.

Intente cambiar el modo de apagado a OnLastWindowClose o OnExplicitShutdown.

De MSDN:

OnMainWindowClose: Una aplicación se cierra cuando cualquiera de la ventana principal se cierra, o de apagado se llama.
OnExplicitShutdown: Una aplicación se apaga solo cuando se llama a Apagado.

+0

Gracias Zach. Esto fue correcto No entendí que mi ventana de diálogo me robó la referencia de MainWindow aunque reinicié el objeto MainWindow unas líneas más abajo. – TWood

+0

@TWood: De nada. Creo que lo confuso es probablemente que 'OnMainWindowClose' no signifique cuando la ventana' MainWindow' se cierra, sino cuando se cierra la primera ventana abierta. –

Cuestiones relacionadas