当用户从文件菜单中单击退出菜单项时,应该如何退出应用程序?

我试过:

this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();

还有很多其他的。没有什么工作。


当前回答

如果你真的需要它来关闭,你也可以使用Environment.Exit(),但这一点都不优雅(更像是结束进程)。

使用方法如下:

Environment.Exit(0)

其他回答

private void _MenuExit_Click(object sender, RoutedEventArgs e)
{
   System.Windows.Application.Current.MainWindow.Close();
}

//Override the onClose method in the Application Main window

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    MessageBoxResult result =   MessageBox.Show("Do you really want to close", "",
                                          MessageBoxButton.OKCancel);
    if (result == MessageBoxResult.Cancel)
    {
       e.Cancel = true;
    }
    base.OnClosing(e);
}

Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

if (System.Windows.Forms.Application.MessageLoop) 
{
    // WinForms app
    System.Windows.Forms.Application.Exit();
    system.Environment.Exit(1);
}

Try

App.Current.Shutdown();

对我来说

Application.Current.Shutdown(); 

没有工作。

如果你使用application . current . shutdown()来退出应用程序,你可能会得到一个System. current . shutdown()。调用线程不能访问该对象,因为它属于另一个线程。如果从不同的线程调用它。要解决这个问题,您可以像这样包装调用

Application.Current.Dispatcher.Invoke(() => Application.Current.Shutdown());