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

我试过:

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

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


当前回答

App.Current.Shutdown (); 把它写在你需要的地方。

其他回答

根据需要使用以下任何一种方法:

1.

 App.Current.Shutdown();
OR
 Application.Current.Shutdown();

2.

 App.Current.MainWindow.Close();
OR
 Application.Current.MainWindow.Close();

以上所有的方法都会调用Window类的关闭事件,并且执行可能会在某个时候停止(因为通常应用程序会设置像'are you sure?或“关闭前要保存数据吗?”,在窗口完全关闭之前)

3.但如果您想立即终止应用程序而不发出任何警告。使用下面的

Environment.Exit(0);

如果你想从另一个没有创建应用程序对象的线程中退出,请使用:

Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

App.Current.Shutdown (); 把它写在你需要的地方。

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