Simplified ICommand implementation in WPF MVVM application


Saral framework which provides utilities to ease the MVVM implementation in Silverlight and WPF application presents SaralCommand. This is a simplified version of DelegateCommand which  was introduced with  Microsoft Composite Application Library

SaralCommand makes things very simple by allowing developer to concentrate on business logic and get rid of ICommand interface implementation. I have just checked-in the code of SaralCommand in the Saral framework hosted on Codeplex

This post provides an example implementation of SaralCommand.  I have taken a Prism and MVVM based WPF application posted in my previous blog post to work with SaralCommand.

Environment: Visual Studio 2010

Source code : Download


    public class SaralCommand : ICommand
    {

        readonly Action<object> executeLogic;

        readonly Predicate<object> canExecuteCondition;

        readonly bool predicateFlag;

        public SaralCommand(Action<object> executeLogic, Predicate<object> canExecuteCondition)
        {
            this.executeLogic = executeLogic;
            this.canExecuteCondition = canExecuteCondition;
            this.predicateFlag = false;
        }

        public SaralCommand(Action<object> executeLogic)
        {
            this.executeLogic = executeLogic;
            this.canExecuteCondition = null;
            this.predicateFlag = true;
        }

        public bool CanExecute(object parameter)
        {
            if (predicateFlag == false)
            {
                return canExecuteCondition(parameter);
            }

            return true;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            executeLogic(parameter);
        }
    }




Illustration of SaralCommand

        private Saral.Command.SaralCommand saralCommand;



        public ICommand SearchCommand
        {
            get
            {
                return saralCommand;
            }         
        }



        /// <summary>
        ///  Constructor of view model
        /// </summary>
        /// <param name="eventAggregator"></param>
        public SearchViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;

            saralCommand = new SaralCommand(param => OnSearch(title), param => CanSearch());
        }

        private void OnSearch(object title)
        {
            IDictionary<string, string> searchInputDictionary = new Dictionary<string, string>(1);
            searchInputDictionary.Add("title",(string)title);
            eventAggregator.GetEvent<SearchTopicPropagationEvent>().Publish(searchInputDictionary);

        }

        private bool CanSearch()
        {
            return true;
        }


Environment: Visual Studio 2010

Source code : Download




No comments: