【问题标题】:How to exit a wpf application when Application.Current is NULL?当 Application.Current 为 NULL 时如何退出 wpf 应用程序?
【发布时间】:2014-10-23 08:48:52
【问题描述】:

当我需要退出WPF Application 时,我通常使用Application.Current.Shutdown(),但有时Application.CurrentNULL,我也尝试过Environment.Exit(0),但它不能正常工作。 当Application.CurrentNULL 时,还有其他方法可以退出WPF Applicationspecialy 吗?

这是我的代码:

 public partial class App : Application
 {
    protected override void OnStartup(StartupEventArgs e)
    {
      if (something)
        {
          continue....
        }
      else
        {
        Application.Current.Shutdown()
        }
  }

【问题讨论】:

  • 从什么上下文中调用 Application.Current?你能发布你正在使用的代码吗?
  • 我想关闭我的整个应用程序!对于任何AppDomain,只有一个System.Windows.Application
  • 你有没有试过 this.Close(); ??
  • @apomene:你这是什么意思?我在App.xaml 中使用此代码,而在此处不使用此代码!!!
  • 我第一次听说Application.Current 可以为空。至少在加载主窗口之后,Application.Current 没有理由为空。

标签: wpf


【解决方案1】:
System.Diagnostics.Process.GetCurrentProcess().Kill();

这会立即终止您的应用程序。

【讨论】:

    【解决方案2】:

    这显然是每个设计。为什么启动尚未完成时会有可用的应用程序。就这样做吧。

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            if (false)
            {
                //
            }
            else
            {
                this.Shutdown();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多