当用户从文件菜单中单击退出菜单项时,应该如何退出应用程序?
我试过:
this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();
还有很多其他的。没有什么工作。
当用户从文件菜单中单击退出菜单项时,应该如何退出应用程序?
我试过:
this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();
还有很多其他的。没有什么工作。
当前回答
Caliburn微调味
public class CloseAppResult : CancelResult
{
public override void Execute(CoroutineExecutionContext context)
{
Application.Current.Shutdown();
base.Execute(context);
}
}
public class CancelResult : Result
{
public override void Execute(CoroutineExecutionContext context)
{
OnCompleted(this, new ResultCompletionEventArgs { WasCancelled = true });
}
}
其他回答
根据需要使用以下任何一种方法:
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.ShutDown();或. exit()消息。
Application是一个静态类。它不指向当前应用程序。 你需要进入当前应用程序,然后像这样关闭它:
Application.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);
}
Try
App.Current.Shutdown();
对我来说
Application.Current.Shutdown();
没有工作。