Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this class implements the functionality of Prism's CompositeCommand
- public class CompositeCommand : ICommand
- {
- List<ICommand> innerCommands = new List<ICommand>();
- public CompositeCommand() : this(_ => true)
- {
- }
- public CompositeCommand(Func<ICommand, bool> shouldExecute)
- {
- this.shouldExecute = shouldExecute;
- }
- public void RegisterCommand(ICommand command)
- {
- innerCommands.Add(command);
- command.CanExecuteChanged += OnCanExecuteChanged;
- }
- public void UnregisterCommand(ICommand command)
- {
- command.CanExecuteChanged -= OnCanExecuteChanged;
- innerCommands.Remove(command);
- }
- private void OnCanExecuteChanged(object sender, EventArgs args)
- {
- OnCanExecuteChanged();
- }
- protected virtual void OnCanExecuteChanged()
- {
- var copy = CanExecuteChanged;
- if (copy != null)
- copy(this, new EventArgs());
- }
- public bool CanExecute(object parameter)
- {
- return innerCommands.Where(ShouldExecute).Select(c => c.CanExecute(parameter)).AllTrue();
- }
- public event EventHandler CanExecuteChanged;
- public void Execute(object parameter)
- {
- var commandsToExecute = innerCommands.Where(ShouldExecute).Materialize();
- foreach (var command in commandsToExecute)
- command.Execute(parameter);
- }
- Func<ICommand, bool> shouldExecute;
- protected virtual bool ShouldExecute(ICommand command)
- {
- return shouldExecute(command);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement