Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login Wpf ...is it right?

Tags:

c#

.net

wpf

i have a WPF Application with a LoginWindow to access,so i create a Splash Screen for this Login window as follow :

- in App.xaml

< Application x:Class="WPF.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="Application_Startup"
    />

-in App.xaml.cs:

      private void Application_Startup(object sender, StartupEventArgs e)
  {
         Login login = new Login();
        login.Show();
  }

-and in Login.xaml.cs if the log in is succesful :

PrimaryWindow mainWindow= new PrimaryWindow ();

Application.Current.MainWindow = mainWindow;

this.Close();

mainWindow.Show();

.This code is right but sincerely with my poor knowledge i don't know that's a good method to apply for a Login Window or not and i don't know if this method can be "dangerous" for my application that store data from a database and has many features , so i ask you if my way is good or not and if you have a better way can you suggest or show me that?

Thanks for your attention.

Have a lucky day.

like image 266
JayJay Avatar asked Jun 09 '09 03:06

JayJay


1 Answers

I would handle this with 2 windows and an Application_Startup method. Here is what my app (which has a similar login idea) looks like:

/// In App.xaml.cs
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application {
    private MainWindow main = new MainWindow();
    private LoginWindow login = new LoginWindow();

    private void Application_Startup(object sender, StartupEventArgs e) {
        Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
        Application.Current.MainWindow = login;

        login.LoginSuccessful += main.StartupMainWindow;
        login.Show();
    }
}

/// In LoginWindow.xaml.cs
/// <summary>
/// Interaction logic for LoginWindow.xaml
/// </summary>
public partial class LoginWindow : Window {
    internal event EventHandler LoginSuccessful;

    public LoginWindow() { InitializeComponent(); }

    private void logInButton_Click(object sender, RoutedEventArgs e) {
        if ( // Appropriate Login Check Here) {
            LoginSuccessful(this, null);
            Close();
        } else {
            // Alert the user that login failed
        }
    }
}

/// In MainWindow.xaml.cs
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
    public MainWindow() { InitializeComponent(); }

    internal void StartupMainWindow(object sender, EventArgs e) {
        Application.Current.MainWindow = this;
        Show();
    }
}

This allows the user to close the application simply by closing the login window (i.e. not logging in at all) or by closing the main window AFTER they have used it for a while.

like image 115
mikeschuld Avatar answered Oct 05 '22 10:10

mikeschuld