Stupidly simple question I can't figure out.
I have a WPF application, and I want to display a Logon dialog box first (before the main window shows up).
If the user logs on successfully, then I want to show the main window, if not, I want to exit the application.
How does one do this correctly?
I think I figured out what I was trying to do.
1) I needed to set the "StartupUri" in the App.xaml to "Logon.xaml", where Logon.xaml is my logon window.
2) in the LogonButton_Click event handler, I added the following
if (blnAuthenticateSuccessful) { MainWindow main = new MainWindow(); App.Current.MainWindow = main; this.Close(); main.Show(); }
This seems to accomplish what I want.
If you wanted a new window to appear to allow the user to enter their login information, then I have added some code below. However, creating a true Modal Dialog box is a bit more complicated in WPF so I haven't explained it here. There is information about modal dialog boxes in WPF here: http://msdn.microsoft.com/en-us/library/aa969773.aspx
From the MainWindow you can open the login window and hide the main window with this:
// Code for MainWindow // Create a new instance of the login window and then show it LoginWindow loginWindow = new LoginWindow(); loginWindow.Show(); // Hide the MainWindow until later this.Hide();
Then use this on the login page to show the main window again once the user has logged in:
// Code for Login window // This code finds the main window again and shows it Application.Current.MainWindow.Show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With