C# 从 App.xaml.cs 关闭 WPF 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/606043/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:05:20  来源:igfitidea点击:

Shutting down a WPF application from App.xaml.cs

c#wpfapplication-shutdown

提问by Joey

I am currently writing a WPF application which does command-line argument handling in App.xaml.cs (which is necessary because the Startup event seems to be the recommended way of getting at those arguments). Based on the arguments I want to exit the program at that point already which, as far as I know, should be done in WPF with Application.Current.Shutdown()or in this case (as I am in the current application object) probably also just this.Shutdown().

我目前正在编写一个 WPF 应用程序,它在 App.xaml.cs 中执行命令行参数处理(这是必要的,因为 Startup 事件似乎是获取这些参数的推荐方式)。基于我想在那时退出程序的参数,据我所知,应该在 WPF 中完成,Application.Current.Shutdown()或者在这种情况下(因为我在当前应用程序对象中)可能也只是this.Shutdown().

The only problem is that this doesn't seem to work right. I've stepped through with the debugger and code after the Shutdown()line still gets executed which leads to errors afterwards in the method, since I expected the application not to live that long. Also the main window (declared in the StartupUri attribute in XAML) still gets loaded.

唯一的问题是这似乎不起作用。在该Shutdown()行仍然被执行之后,我已经逐步完成了调试器和代码,这会导致方法之后出现错误,因为我预计应用程序不会存活那么长时间。此外,主窗口(在 XAML 的 StartupUri 属性中声明)仍会加载。

I've checked the documentation of that method and found nothing in the remarks that tell me that I shouldn't use it during Application.Startupor Applicationat all.

我检查了该方法的文档,但在评论中没有发现任何内容告诉我我不应该使用它Application.Startup或根本不应该使用它Application

So, what is the right way to exit the program at that point, i.?e. the Startupevent handler in an Applicationobject?

那么,此时退出程序的正确方法是什么,即 对象中的Startup事件处理程序Application

采纳答案by Jakob Christensen

First remove the StartupUri property from App.xaml and then use the following:

首先从 App.xaml 中删除 StartupUri 属性,然后使用以下内容:

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

        bool doShutDown = ...;

        if (doShutDown)
        {
            Shutdown(1);
            return;
        }
        else
        {
            this.StartupUri = new Uri("Window1.xaml", UriKind.Relative);
        }
    }

回答by FodderZone

If you remove the StartupUri from app.xaml for an application with a MainWindow you need to make sure you make the following call in your OnStartup method otherwise the application will not terminate when your MainWindow closes.

如果从 app.xaml 中删除具有 MainWindow 的应用程序的 StartupUri,则需要确保在 OnStartup 方法中进行以下调用,否则应用程序不会在 MainWindow 关闭时终止。

this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;

@Frank Schwieterman, something along these lines may help you with your console window issue.

@Frank Schwieterman,这些方面的内容可能会帮助您解决控制台窗口问题。