Silverlight 4 ICommand Implementation

Silverlight 4 adds support for ICommand on the ButtonBase and Hyperlink classes. This is great in that the gap between WPF and Silverlight is closed ever so slightly by this addition. Not to mention how well it supports the Presentation Model pattern (or Model-View-ViewModel) without having to use the Prism implementation. The only bummer is that there is no implementation included with the Silverlight 4 runtime. My implementation is below and note that it doesn’t make use of the CanExecuteChanged event handler. I also included an example of this in action.

For those that are unfamiliar with the use ICommand in WPF, it’s an aid in decoupling the GUI code-behind from the presentation logic. When combined with the PresentationModel pattern (or M-V-VM or similar model) makes a GUI application decoupled and testable. Any controls inheriting from ButtonBase or Hyperlink will contain Command and CommandParameter properties enabling this functionality. The ICommand interface has only 3 members, Execute and CanExecute methods as well as a CanExecuteChanged event. The Execute method is called when the button or link is pressed. The CanExecute method is called when the control is initialized and when the CanExecuteChanged event is fired and determines if the control is enabled based upon the value returned.

public class Command : ICommand
{
    private readonly Action<object> executeDelegate;
    private readonly Func<object, bool> canExecuteDelegate;

    public event EventHandler CanExecuteChanged;

    public Command(Action<object> executeDelegate)
    {
         this.executeDelegate = executeDelegate;
    }

    public Command(Func<object, bool> canExecuteDelegate,
 		   Action<object> executeDelegate)
    {
         this.canExecuteDelegate = canExecuteDelegate;
         this.executeDelegate = executeDelegate;
    }

    public bool CanExecute(object parameter)
    {
         if (canExecuteDelegate == null)
         {
              return false;
         }

         return canExecuteDelegate(parameter);
    }

    public void Execute(object parameter)
    {
         if (executeDelegate == null)
         {
              return;
         }

         executeDelegate(parameter);
     }
}

The constructor(s) allow you to pass in delegate(s) that determine what happens when the CanExecute and Execute methods are called.

 public class MyPresentationModel : IMyPresentationModel
 {
     public event EventHandler CommandExecuted;

     public MyPresentationModel()
     {
         MyCommand = new Command(CanExecuteCommand, ExecuteCommand);
     }

     public ICommand MyCommand { get; set; }

     public bool CanExecuteCommand(object parameter)
     {         
          // Logic to determine if button is enabled goes here.
         return true;
     }

     publicvoid ExecuteCommand(object parameter)
     {
         if (CommandExecuted != null)
         {
             CommandExecuted(this, EventArgs.Empty);
         }
     }
}

In the above example, the Command class takes in two delegate parameters in its constructor. The command is exposed as a property that the Button control can bind to.

 <Button Content="Press this button" Command="{Binding MyCommand}" /> 

Here is the xaml needed to binding the Command property of the button to the Command property of the presentation model.

Advertisements

2 thoughts on “Silverlight 4 ICommand Implementation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s