Shutting down a WPF application from App.xaml.cs

a 夏天 提交于 2019-11-28 06:45:01

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

    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);
        }
    }

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!