登录WPF ...是不是?

登录WPF ...是不是?

问题描述:

我有一个登录窗口访问的WPF应用程序,所以我创建了一个闪屏为这个登录窗口如下:

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

- 在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"
    />

- 在App.xaml.cs:

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

- ,并在Login.xaml.cs如果在日志中是成功的:

PrimaryWindow mainWindow= new PrimaryWindow ();

Application.Current.MainWindow = mainWindow;

this.Close();

mainWindow.Show();

。这code是正确的,但真诚地与我那可怜的知识,我不知道这是一个很好的方法来申请一个登录窗口或没有,我不知道这种方法可能是危险的我应用程序存储数据库中的数据,并有许多功能,所以我问你,如果我的方式是好还是不好,如果你有更好的方法可以为您建议还是告诉我了吗?

.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?

感谢您的关注。

有一个幸运的日子。

我会处理这2扇窗户和Application_Startup方法。以下是我的应用程序(其中也有类似的想法登录)是这样的:

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.