wpf 如果用户登录成功,那么我想显示主窗口,如果没有,我想退出应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5708992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If the user logs on successfully, then I want to show the main window, if not, I want to exit the application
提问by Sako73
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).
我有一个 WPF 应用程序,我想先显示一个登录对话框(在主窗口出现之前)。
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?
如何正确地做到这一点?
回答by Sako73
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.
1) 我需要将 App.xaml 中的“StartupUri”设置为“Logon.xaml”,其中 Logon.xaml 是我的登录窗口。
2) in the LogonButton_Click event handler, I added the following
2) 在 LogonButton_Click 事件处理程序中,我添加了以下内容
if (blnAuthenticateSuccessful) {
MainWindow main = new MainWindow();
App.Current.MainWindow = main;
this.Close();
main.Show();
}
This seems to accomplish what I want.
这似乎完成了我想要的。
回答by Joel Kennedy
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
如果您希望出现一个新窗口以允许用户输入他们的登录信息,那么我在下面添加了一些代码。但是,在WPF中创建一个真正的Modal Dialog要稍微复杂一些,所以我没有在这里解释。这里有关于 WPF 中模态对话框的信息: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:
从 MainWindow 您可以打开登录窗口并使用以下命令隐藏主窗口:
// 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();
回答by Dipen Shah
I know its an old question but I had a similar problem and I solved it bit differently so just wanted to add that so in future someone might benefit from it.
我知道这是一个老问题,但我有一个类似的问题,我解决的方法有点不同,所以只是想补充一下,这样将来有人可能会从中受益。
To solve the problem you can change application shutdown mode to OnExplicitShutdown
and then call Application.Shutdown(0) whenever you want to. For example:
要解决此问题,您可以将应用程序关闭模式更改为OnExplicitShutdown
,然后随时调用 Application.Shutdown(0)。例如:
public App()
{
App.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// assume code below is for displaying dialog
if (MessageBox.Show("Continue?", "", MessageBoxButton.YesNo) == MessageBoxResult.No)
App.Current.Shutdown(0);
}
Here in the constructor, I'm changing application shudown mode and calling Shutdown method when I need to.
在构造函数中,我正在更改应用程序关闭模式并在需要时调用 Shutdown 方法。
Caution: When you change ShutdownMode
make sure to call Shutdown
method otherwise your application will be in memory even after main window closes. I've overrided OnClosed
method in my MainWindow to do that:
注意:当您更改时,请ShutdownMode
确保调用Shutdown
方法,否则即使在主窗口关闭后,您的应用程序也会在内存中。我OnClosed
在 MainWindow 中重写了方法来做到这一点:
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
App.Current.Shutdown(0);
}