应用程序的窗口没有边框,所以右角没有退出按钮?如何正确关闭它?
这是我的方式,首先将命令绑定到自定义退出按钮。
<Button Content="Exit" HorizontalAlignment="Left" Margin="327,198,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ExitCommand}"/>
比单击按钮时在 ViewModel 中引发异常。
class ViewModel:NotificationObject
{
public ViewModel()
{
this.ExitCommand = new DelegateCommand(new Action(this.ExecuteExitCommand));
}
public DelegateCommand ExitCommand { get; set; }
public void ExecuteExitCommand()
{
throw new ApplicationException("shutdown");
}
}
在 Application 类中捕获异常
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
try
{
bootstrapper.Run();
}
catch (Exception ex)
{
HandleException(ex);
}
}
private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
HandleException(e.ExceptionObject as Exception);
}
private static void HandleException(Exception ex)
{
if (ex == null)
return;
Environment.Exit(1);
}
}