当用户从文件菜单中单击退出菜单项时,应该如何退出应用程序?
我试过:
this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();
还有很多其他的。没有什么工作。
当用户从文件菜单中单击退出菜单项时,应该如何退出应用程序?
我试过:
this.Dispose();
this.Exit();
Application.ShutDown();
Application.Exit();
Application.Dispose();
还有很多其他的。没有什么工作。
当前回答
如果你使用application . current . shutdown()来退出应用程序,你可能会得到一个System. current . shutdown()。调用线程不能访问该对象,因为它属于另一个线程。如果从不同的线程调用它。要解决这个问题,您可以像这样包装调用
Application.Current.Dispatcher.Invoke(() => Application.Current.Shutdown());
其他回答
如果你想从另一个没有创建应用程序对象的线程中退出,请使用:
Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
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 });
}
}
根据我的理解,Application.Current.Shutdown()也有它的缺点。
如果你想显示一个确认窗口让用户确认是否退出,Application.Current.Shutdown()是不可逆的。
在xaml代码中,你可以调用一个预定义的SystemCommand:
Command="SystemCommands.MinimizeWindowCommand"
我认为这应该是首选的方式……