nicht so komplex tatsächlich (aber immer noch, saugt M $ für sie nicht bereitstellt). Hier gehen Sie:
public static class MyCommands
{
private static readonly ICommand appCloseCmd = new ApplicationCloseCommand();
public static ICommand ApplicationCloseCommand
{
get { return appCloseCmd; }
}
}
//===================================================================================================
public class ApplicationCloseCommand : ICommand
{
public event EventHandler CanExecuteChanged
{
// You may not need a body here at all...
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return Application.Current != null && Application.Current.MainWindow != null;
}
public void Execute(object parameter)
{
Application.Current.MainWindow.Close();
}
}
Und der Körper der AplicationCloseCommand.CanExecuteChanged
Event-Handler nicht sein kann sogar erforderlich.
Sie verwenden es, wie so:
<MenuItem Header="{DynamicResource MenuFileExit}" Command="MyNamespace:MyCommands.ApplicationCloseCommand"/>
Prost!
(Sie können sich nicht vorstellen, wie es mir lange gedauert hat, diesen Befehl Sachen selbst zu entdecken ...)
Es ist wahrscheinlich Application.Current.Shutdown(); als Application.Current.Exit(); ist ein Ereignis. – Vitalij
Das stimmt, danke. – Jeremy