WPF Application_Startup 事件将永远运行,除非我创建一个窗口并显示它
如果我打开一个窗口并关闭它,WPF 应用程序就会关闭,但如果我没有打开任何窗口,它会保持运行状态,直到我明确关闭它。这是一个快速示例应用程序:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Count() != 0)
{
var mw = new MainWindow();
mw.ShowDialog();
}
}
}
如果您为该应用程序提供命令行参数,它将打开一个窗口,并在您关闭窗口后立即关闭;如果您不提供参数,它将不会关闭,必须通过任务管理器终止。
WPF Application Shuts Down if I open a window and close it, but if I open no windows it stays running until I explicitly close it. Here is a quick sample application:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Count() != 0)
{
var mw = new MainWindow();
mw.ShowDialog();
}
}
}
If you supply this app a command-line argument it will open a window and close down as soon as you close the window; if you don't supply an argument it will not close and must be killed via task manager.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 http://msdn.microsoft.com/en- us/library/system.windows.application.shutdownmode.aspx。
默认关闭模式为OnLastWindowClose。
Check http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx.
The default shutdown mode is OnLastWindowClose.
显然,这与 WPF 如何管理其关闭有关,需要设置 ShutdownMode 属性:
Apparently, this has to do with how WPF manages its shutdown, there is a ShutdownMode property that needs to be set: