T_T*_*T_T 2 .net wpf prism mvvm
应用程序的窗口没有边框,所以右边没有退出按钮?如何以正确的方式关闭它?
这是我的方式,fisrt将命令绑定到自定义退出按钮.
<Button Content="Exit" HorizontalAlignment="Left" Margin="327,198,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ExitCommand}"/>
Run Code Online (Sandbox Code Playgroud)
单击按钮时在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");
}
}
Run Code Online (Sandbox Code Playgroud)
在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);
}
}
Run Code Online (Sandbox Code Playgroud)
只是可以使用Application.Current.Shutdown()?
public void ExecuteExitCommand()
{
Application.Current.Shutdown();
}
Run Code Online (Sandbox Code Playgroud)
使用Exception作为通信机制似乎很奇怪.
如果您不想因任何原因在VM中调用ShutDown(),请使用Messenger(在Prism中EventAggregator)发送自定义消息,您可以从Application Class或MainWindow的代码隐藏中订阅它并调用它Application.Current.Shutdown()