|
///
/// Copyright(C) MixModes Inc. 2010
///
using System;
using System.Windows.Input;
using MixModes.Synergy.Utilities;
namespace MixModes.Synergy.VisualFramework.Commands
{
/// <summary>
/// Base class for commands
/// </summary>
public class CommandBase : ICommand
{
// Private members
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
/// <summary>
/// Initializes a new instance of the <see cref="CommandBase"/> class.
/// </summary>
/// <param name="execute">The execute.</param>
public CommandBase(Action<object> execute)
: this (execute, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CommandBase"/> class.
/// </summary>
/// <param name="execute">The execute.</param>
/// <param name="canExecute">The can execute.</param>
public CommandBase(Action<object> execute, Predicate<object> canExecute)
{
Validate.NotNull(execute, "execute");
_execute = execute;
_canExecute = canExecute;
}
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Defines the method that determines whether the command can execute in its current state.
/// </summary>
/// <param name="parameter">Data used by the command.
/// If the command does not require data to be passed, this object can be set to null.</param>
/// <returns>
/// true if this command can be executed; otherwise, false.
/// </returns>
public bool CanExecute(object parameter)
{
return _canExecute != null ? _canExecute(parameter) : true;
}
/// <summary>
/// Defines the method to be called when the command is invoked.
/// </summary>
/// <param name="parameter">Data used by the command.
/// If the command does not require data to be passed, this object can be set to null.</param>
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Ashish worked for Microsoft for a number of years in Microsoft Visual Studio (Architect edition) and Windows Live division as a developer. Before that he was a developer consultant mainly involved in distributed service development / architecture. His main interests are distributed software architecture, patterns and practices and mobile device development.
Currently Ashish serves as a Technical Lead at RIM leading next generation BlackBerry media experience and also runs his own company MixModes Inc. specializing in .NET / WPF / Silverlight technologies. You can visit MixModes at http://mixmodes.com or follow it on Twitter @MixModes
In his free time he is an avid painter, hockey player and enjoys travelling. His blog is at: http://ashishkaila.serveblog.net