Skip to content

Instantly share code, notes, and snippets.

@runceel
Created April 21, 2012 13:38
Show Gist options
  • Save runceel/2437116 to your computer and use it in GitHub Desktop.
Save runceel/2437116 to your computer and use it in GitHub Desktop.
WinRT ICommand implementation
using System;
using System.Collections.Generic;
using System.Windows.Input;
namespace Application3.Common
{
class DelegateCommand : ICommand
{
private static readonly Action EmptyExecute = () => { };
private static readonly Func<bool> EmptyCanExecute = () => true;
private Action execute;
private Func<bool> canExecute;
public DelegateCommand(Action execute, Func<bool> canExecute = null)
{
this.execute = execute ?? EmptyExecute;
this.canExecute = canExecute ?? EmptyCanExecute;
}
public void Execute()
{
this.execute();
}
public bool CanExecute()
{
return this.canExecute();
}
bool ICommand.CanExecute(object parameter)
{
return this.CanExecute();
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
var h = this.CanExecuteChanged;
if (h != null)
{
h(this, EventArgs.Empty);
}
}
void ICommand.Execute(object parameter)
{
this.Execute();
}
}
static class CommandExetensions
{
public static void RaiseCanExecuteChanged(this ICommand self)
{
var delegateCommand = self as DelegateCommand;
if (delegateCommand == null)
{
return;
}
delegateCommand.RaiseCanExecuteChanged();
}
public static void RaiseCanExecuteChanged(this IEnumerable<ICommand> self)
{
foreach (var command in self)
{
command.RaiseCanExecuteChanged();
}
}
public static void RaiseCanExecuteChanged(params ICommand[] commands)
{
commands.RaiseCanExecuteChanged();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment