I expose CompositeCommands that clients can register to for interesting global "events", e.g.
public static class HostCommands
{
private static readonly CompositeCommand Shutdown = new CompositeCommand();
public static CompositeCommand ShutdownCommand
{
get { return Shutdown; }
}
}
I trigger the shutdown command in my shell, e.g.
public Shell()
{
InitializeComponent();
Closing += (sender, e) =>
{
if (HostCommands.ShutdownCommand.CanExecute(e))
HostCommands.ShutdownCommand.Execute(e);
};
}
And clients can register as follows, e.g
public SomeViewModel(IEventAggregator eventService)
{
//blah, blah, blah...
HostCommands.ShutdownCommand.
RegisterCommand(new DelegateCommand<object>(_ => Save()));
}
Update
I do not handle the cancel scenario, but you could implement this via the object which is passed to the command. For instance in the above code I pass in a CancelEventArgs which clients could manipulate i.e. by setting Cancel=true. I could inspect this value in my Shell closed event handler after the command has execute to derive whether I should cancel closing the shell. This pattern can be expanded on.