title: Commands using Action<> and Func<>
body: <p>I am working through <a href="http://www.packtpub.com/mvvm-survival-guide-for-enterprise-architectures-in-silverlight-and-wpf/book" rel="nofollow">MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF</a> and have hit a snag in the Command section.  Specifically, it creates a command based on an Action&lt;object> and Func&lt;object, bool>.  At a point where I'm supposed to 'build and run the application', I am instead getting compile errors.</p>
  
  <p>The command stuff:</p>
  
  <pre><code>public class Command : ICommand
  {
    private readonly Action&lt;object&gt; _execute;
    private readonly Func&lt;object, bool&gt; _canExecute;
    public Command(Action&lt;object&gt; execute, Func&lt;object, bool&gt; canExecute)
    {
      _execute = execute;
      _canExecute = canExecute;
    }
   public void Execute(object parameter)
    {
      _execute(parameter);
    }
    public bool CanExecute(object parameter)
    {
      return (_canExecute == null) || _canExecute(parameter);
    }
    ...
  }
  </code></pre>
  
  <p>The method call stuff:</p>
  
  <pre><code>private Command _showDetailsCommand;
  public Command ShowDetailsCommand
  {
    get 
    { 
        return _showDetailsCommand 
               ?? (_showDetailsCommand 
                  = new Command(ShowCustomerDetails, IsCustomerSelected));
     }
  }
  public void ShowCustomerDetails()
  {
    if (!IsCustomerSelected()){
      throw new InvalidOperationException("Unable to display customer details. "
                  + "No customer selected.");
    }
    CustomerDetailsTabViewModel customerDetailsViewModel = 
              GetCustomerDetailsTab(SelectedCustomerID);
    if (customerDetailsViewModel == null)
    {
      customerDetailsViewModel 
           = new CustomerDetailsTabViewModel
                 (_dataProvider, 
                  SelectedCustomerID);
      Tabs.Add(customerDetailsViewModel);
    }
    SetCurrentTab(customerDetailsViewModel);
  }
  private bool IsCustomerSelected()
  {
    return !string.IsNullOrEmpty(SelectedCustomerID);
  }
  </code></pre>
  
  <p>I get wavy blue lines under the <code>new Command(ShowCustomerDetails, IsCustomerSelected))</code> bit with a 'The best overloaded match for <code>Northwind.Application.Command.Command(System.Action&lt;object&gt;, System.Func&lt;object, bool&gt;)</code> has some invalid arguments'.</p>
  
  <p>When I try to compile, I get the above error, plus two messages:</p>
  
  <pre><code>Argument 1: Cannot convert from method group to System.Action&lt;object&gt;
  Argument 2: Cannot convert from method group to System.Func&lt;object, bool&gt;
  </code></pre>
  
  <p>Now, I know <em>a lot more</em> about Actions and Funcs than I did yesterday, and can almost bypass the errrors by changing the command declaration to:</p>
  
  <pre><code>private readonly Action _execute;
  private readonly Func&lt;bool&gt; _canExecute;
  </code></pre>
  
  <p>and doing similar throughout the code, but then I get an error saying I haven't implemented <code>ICommand</code> properly.</p>
  
  <p>To save my forehead/the nearest wall, can somebody either tell me what I haven't done right so I can fix it, or that the given (book) code is leading me awry, so I can move on.</p>