Shutting down a WPF application from App.xaml.cs

后端 未结 3 1479
谎友^
谎友^ 2020-12-08 18:51

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 w

3条回答
  •  囚心锁ツ
    2020-12-08 19:54

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

提交回复
热议问题