Click here to Skip to main content
15,903,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 wpf windows MainWindow.xaml and LoginWindow.xaml. In the file app.g.cs I changed
C#
this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative)
this.StartupUri = new System.Uri("LoginWindow.xaml", System.UriKind.Relative)

Now LoginWindow runs at the start up. On log in button click I am using showdialog to show MainWindow and hiding the log in form.

Are there any better ways? because my main program us running as a dialog and I am not sure if it makes any difference?
Posted
Updated 17-Apr-13 2:35am
v2

You CANNOT change the app.g.cs file - this file is auto generated by Visual Studio - “Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.”!
Hook to the Application.Startup Event[^] and do your logic there.
Also check the Application.ShutdownMode Property[^].
Sample code used in my WPF project:
C#
private void Application_Startup(object sender, StartupEventArgs e)
{
  // ... init code here...

  // Create new LoginWindow
  var dlg = new LoginWindow();

  // Need to CLEAR MainWindow property
  MainWindow = null;

  // Show LoginWindow as dialog
  if (dlg.ShowDialog() == true) // User is logged-in (dialog returns DialogResult = true)
  {
    // Assign and show MainWindow
    MainWindow = new MainWindow();
    MainWindow.Show();
  }
}
 
Share this answer
 
v2
Comments
[no name] 17-Apr-13 9:36am    
Nicely spotted.
missak boyajian 20-Apr-13 7:38am    
So on the application.startup event I showed the login form dialog. But when i click cancel the main windows does not show up why ?
I did like this.
StartupUri="MainWindow.xaml" Startup="Application_Startup_1">
In the Application_Startup method I showed login form dialog.
Matej Hlatky 22-Apr-13 4:01am    
Check my sample code in updated answer.
missak boyajian 22-Apr-13 5:39am    
Thanks it worked
That does not sound correct. Your main window should always be presented by the normal Show() method, and the login page should use ShowDialog(). In that way the application is paused until the login is completed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900