David Sandor

Build succeeded.

DelegateCommand class for Windows Phone 7 and 8

clock May 4, 2013 10:27 by author dsandor

First off, I did not author this from scratch.  Instead, I added some functionality to this class that WindowsPhoneGeek published. I wanted to add CanExecute() and Execute() methods that take no parameter.   Most of the commands I use in my MVVM apps tend to be based on the state of the ViewModel anyway and thus I do not need the parameters.

Here is the DelegateCommand.cs class and the cmds snippet that lets you quickly add a command to any ViewModel.

      

Command code would look like the following:

public MainPageViewModel()
{
    LoginCommand = new DelegateCommand(OnLoginCommand, CanExecuteLoginCommand);
}

public ICommand LoginCommand { get; set; }

private void OnLoginCommand()
{

}

private bool CanExecuteLoginCommand()
{
    return false;
}

The code for the DelegateCommand.cs looks like this:

public class DelegateCommand : ICommand
{
    Func<object, bool> canExecute;
    Action<object> executeAction;
 
    Func<bool> canExecuteSimple;
    Action executeActionSimple;
 
    public DelegateCommand(Action<object> executeAction)
        : this(executeAction, null)
    {
    }
 
    public DelegateCommand(Action executeAction)
        : this(executeAction, null)
    {
    }
 
    public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
    {
        if (executeAction == null)
        {
            throw new ArgumentNullException("executeAction");
        }
        this.executeAction = executeAction;
        this.canExecute = canExecute;
    }
 
    public DelegateCommand(Action executeAction, Func<bool> canExecute)
    {
        if (executeAction == null)
        {
            throw new ArgumentNullException("executeAction");
        }
        this.executeActionSimple = executeAction;
        this.canExecuteSimple = canExecute;
    }
 
    public bool CanExecute(object parameter)
    {
        bool result = true;
        Func<object, bool> canExecuteHandler = this.canExecute;
        if (canExecuteHandler != null)
        {
            result = canExecuteHandler(parameter);
            return result;
        }
 
        Func<bool> canExecuteHandlerSimple = this.canExecuteSimple;
        if (canExecuteHandlerSimple != null)
        {
            result = canExecuteHandlerSimple();
        }
 
        return result;
    }
 
    public bool CanExecute()
    {
        bool result = true;
        Func<bool> canExecuteHandler = this.canExecuteSimple;
        if (canExecuteHandler != null)
        {
            result = canExecuteHandler();
        }
 
        return result;
    }
 
 
    public event EventHandler CanExecuteChanged;
 
    public void RaiseCanExecuteChanged()
    {
        EventHandler handler = this.CanExecuteChanged;
        if (handler != null)
        {
            handler(this, new EventArgs());
        }
    }
 
    public void Execute(object parameter)
    {
        // Default to action that takes parameter.
        if (this.executeAction != null)
        {
            this.executeAction(parameter);
            return;
        }
 
        // Fallback to parameterless delegate.
        if (this.executeActionSimple != null)
        {
            this.executeActionSimple();
            return;
        }
    }
 
    public void Execute()
    {
        this.executeActionSimple();
    }
}


Solved: Delegate to an instance method cannot have null ‘this’.

clock September 12, 2009 01:49 by author dsandor

So this was a really weird one.  The solution is simple but I figured I’d post it in case someone out there was stumped.

Service cannot be started. System.ArgumentException: Delegate to an instance method cannot have null 'this'.

The problem was with this line of code:

readerThread        = new Thread(new ThreadStart(worker.DoWork));

I was instantiating a new thread that executes the DoWork instance method of the ‘worker’ object.  The problem was that I failed to instantiate the worker object.



About the author

David Sandor is a Software Architect working in Chicago, IL.  My development focuses around the Microsoft Stack including Azure, AppFabric, Silverlight, WPF, .NET Framework, and various mobile devices including iOS (iPhone/iTouch), Android, Windows Mobile and Windows Phone 7.

Month List

Sign in