2009-06-09 30 views
10

我有一个登录窗口访问的WPF应用程序,所以我创建此登录窗口,如下一个闪屏:登录Wpf ...是不是?

- 在App.xaml中- 在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" 
    /> 

的.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(); 

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

感谢您的关注。

祝您有幸运的一天。

回答

15

我会用2个窗口和一个Application_Startup方法来处理这个问题。这里是我的应用程序(其中有一个类似登录想法)的样子:

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

这使用户可以通过关闭登录窗口(即在所有不记录),或者通过关闭主只需关闭应用程序窗口后他们已经使用了一段时间。

1

就我个人而言,我认为登录窗口关闭主应用程序窗口并不是一个好主意。为什么不做这样的事情?

PrimaryWindow mainWindow= new PrimaryWindow(); 

Application.Current.MainWindow.Hide(); // or something, don't know the exact syntax 

mainWindow.Closed += delegate{Application.Current.MainWindow.Show(); }; 

mainWindow.Show(); 
+0

嗨弗朗西斯, 感谢回复我,我会问你为什么不好杀死应用程序主窗口?因为我的无知,我不这样认为。 你的代码很好,但我不喜欢隐藏LoginWindow。 非常感谢您的帮助;)。 – JayJay 2009-06-09 06:50:14

+0

嗨JayJay, 在我看来,你做以下几点: 1)创建一个登录窗口 2)杀死的应用程序主窗口 3)设置登录窗口的应用程序主窗口 上午我正确?如果是这样,当你杀死主窗口时,在登录窗口关闭后你会显示什么? – user112889 2009-06-09 20:57:16

+0

是的,没错, 在LoginWindow之后,我将显示MainApplication Client来存储,编辑,保存,删除etc。数据,其中用户从基于角色的安全性获得使用该应用程序的服务器。 Thaks – JayJay 2009-06-10 02:21:17

0

看起来对我来说很好 - 这段代码本身并不是'危险的'。什么可能是危险的是你如何收集和处理用户的登录窗口的凭据,但你没有发布代码,所以我不能发表评论。

您可以通过在应用程序标记中使用StartupUri="Login.xaml"而不是指向事件处理程序来优化此代码,它会以较少的代码实现相同的效果。

1

你的代码似乎没问题,但是我个人喜欢在一个地方放置东西 - 登录应该只是担心登录,而不是是否显示主屏幕或关闭应用程序。所以,我的解决办法是这样的:

的App.xaml

<Application 
    .... 
    StartupUri="Forms/MainWindow.xaml" > 
.... 
</Application 

App.xaml.cs

protected override void OnStartup(StartupEventArgs e) 
    { 
     base.OnStartup(e); 

     //Without the next line your app would've ended upon closing Login window: 
     ShutdownMode = ShutdownMode.OnExplicitShutdown; 
     //Authenticate user (if canceled returns 'false') 
     LoginScreen wndLogin = new LoginScreen(); 
     if (wndLogin.ShowDialog() == false) 
     { 
      Shutdown(); 
     } 
     else 
     { 
      //if you have some cache to load, then show some progress dialog, 
      //or welcome screen, or whatever... 
      //after this, the MainWindow executes, so restore the ShutdownMode, 
      //so the app ends with closing of main window (otherwise, you have to call 
      //Applicaiton.Current.Shutdown(); explicitly in Closed event of MainWindow) 
      ShutdownMode = ShutdownMode.OnMainWindowClose; 
     } 
    } 

希望这有助于。

1

我通常使用服务对象来处理与登录相关的任何事情。我也有一个Session和Credentials对象(定制的),它通过程序持久化。所有登录逻辑都存储在SessionService中。

static void Main(string[] args) 
{ 
    ServiceManager.RegisterService<SessionService>(); 

    Session session; 
    if(ServiceManager.GetService<SessionService>().CreateSession(out session) == CreateSessionResult.Success) 
    { 
     MainWindow window = new MainWindow(); 
     window.SetSession(session); 
     Application.Run(window); 
    } 
    ServiceManager.UnregisterService<SessionService>(); 
} 
4

林的情况下,使用这一

void App_Startup(object sender, StartupEventArgs e) 
    { 
     this.MainWindow = new MainWindow(); 

     LoginWindow loginWindow = new LoginWindow(); 
     if (loginWindow.ShowDialog() ?? false) 
     { 
      this.MainWindow.Show(); 
     } 
     else 
     { 
      this.Shutdown(); 
     } 
    } 

或者,这时候主窗口必须创建检查凭据。

void App_Startup(object sender, StartupEventArgs e) 
    { 
     this.ShutdownMode = ShutdownMode.OnExplicitShutdown; 

     LoginWindow loginWindow = new LoginWindow(); 
     if (loginWindow.ShowDialog() ?? false) 
     { 
      this.ShutdownMode = ShutdownMode.OnMainWindowClose; 
      this.MainWindow = new MainWindow(); 
      this.MainWindow.Show(); 
     } 
     else 
     { 
      this.Shutdown(); 
     } 
    }