在WPF中设计登录窗口关闭时打开主窗口,自动生成的App.xaml不能满足要求,
1、把App.xaml的属性窗口中的生成操作设定为 无
2、添加Program类
static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { LoginWindow loginForm = new LoginWindow(); loginForm.Init(); bool? rt = loginForm.ShowDialog(); loginForm.Close(); if (rt == true) { Application App = new Application(); App.ShutdownMode = ShutdownMode.OnMainWindowClose; MainWindow m_MianWindow = new MainWindow(); App.MainWindow = m_MianWindow; App.Run(m_MianWindow); } } }
这样就可以满足要求了
参考:http://www.mysjtu.com/page/M0/S613/613036.html
方法二:
最近看一方法,不用添加Program方法即可,代码如下:
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown; LoginWindow window = new LoginWindow(); bool? dialogResult = window.ShowDialog(); if ((dialogResult.HasValue == true) && (dialogResult.Value == true)) { base.OnStartup(e); Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
} else { this.Shutdown(); } } }
通过Application的ShutdownMode控制进程的运行时间。