登录表单如何在WPF中运行? [英] how does the login form work in WPF?

查看:58
本文介绍了登录表单如何在WPF中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个wpf窗口MainWindow.xaml和LoginWindow.xaml。在app.g.cs文件中,我更改了

I have 2 wpf windows MainWindow.xaml and LoginWindow.xaml. In the file app.g.cs I changed

this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative)
this.StartupUri = new System.Uri("LoginWindow.xaml", System.UriKind.Relative)



现在LoginWindow在启动时运行。在登录按钮单击我使用showdialog显示MainWindow并隐藏登录表单。



还有更好的方法吗?因为我的主程序我们作为对话框运行而且我不确定它是否有任何区别?


Now LoginWindow runs at the start up. On log in button click I am using showdialog to show MainWindow and hiding the log in form.

Are there any better ways? because my main program us running as a dialog and I am not sure if it makes any difference?

推荐答案

无法更改app.g.cs文件 - 此文件由Visual Studio自动生成 - 对此文件的更改可能会导致错误的行为,如果重新生成代码,则会丢失。!

挂钩到Application.Startup事件 [ ^ ]并在那里做你的逻辑。

另请查看 Application.ShutdownMode属性 [ ^ ]。

我的WPF项目中使用的示例代码:

You CANNOT change the app.g.cs file - this file is auto generated by Visual Studio - "Changes to this file may cause incorrect behavior and will be lost if the code is regenerated."!
Hook to the Application.Startup Event[^] and do your logic there.
Also check the Application.ShutdownMode Property[^].
Sample code used in my WPF project:
private void Application_Startup(object sender, StartupEventArgs e)
{
  // ... init code here...

  // Create new LoginWindow
  var dlg = new LoginWindow();

  // Need to CLEAR MainWindow property
  MainWindow = null;

  // Show LoginWindow as dialog
  if (dlg.ShowDialog() == true) // User is logged-in (dialog returns DialogResult = true)
  {
    // Assign and show MainWindow
    MainWindow = new MainWindow();
    MainWindow.Show();
  }
}


这听起来不正确。您的主窗口应始终由正常的 Show()方法显示,登录页面应使用 ShowDialog() 。以这种方式,应用程序暂停,直到登录完成。
That does not sound correct. Your main window should always be presented by the normal Show() method, and the login page should use ShowDialog(). In that way the application is paused until the login is completed.


这篇关于登录表单如何在WPF中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆