views:

1039

answers:

2

I am applying the MVVM pattern per Josh Smith and having difficulty. I've been researching the problem here and can't seem to get the syntax quite right.

The code below looks to me like it follows the required syntax, but Visual Studio reports error "Delegate 'System.Action' does not take '2' arguments" on the line indicated.

Can someone see where I am making a mistake? Thanks!
+tom

    RelayCommand _relayCommand_MoveUp;
    public ICommand RelayCommand_MoveUp
    {
      get
      {
        if (_relayCommand_MoveUp == null)
        {
          _relayCommand_MoveUp = new RelayCommand(
          (sender, e) => this.Execute_MoveUp(sender, e),     **ERROR REPORTED HERE**
          (sender, e) => this.CanExecute_MoveUp(sender, e));
          return _relayCommand_MoveUp;
        }
      }
    }

    private void Execute_MoveUp(object sender, ExecutedRoutedEventArgs e)
    {
      if (_selectedFolder != null)
      {
        _selectedFolder.SelectParent();
      }
    }

    private void CanExecute_MoveUp(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = (_selectedFolder != null) && (_selectedFolder.Parent != null);
        }


//And from Josh Smith:

  public class RelayCommand : ICommand
  {
    public RelayCommand(Action<object> execute);
    public RelayCommand(Action<object> execute, Predicate<object> canExecute);

    public event EventHandler CanExecuteChanged;

    [DebuggerStepThrough]
    public bool CanExecute(object parameter);
    public void Execute(object parameter);
  }
+1  A: 

The RelayCommand isn't a RoutedCommand, which I think is where you ending up confused.

The constructors for the Relay command take an Action delegate and optional Predicate delegate. These delegates don't take an EventArgs, just the single Object parameter, which is why you are encountering an error. The predicate also requires a return type of bool, which is the next error you'll get. In the CanExecute predicate instead of setting e.CanExecute like you do with a RoutedCommand you simply return true/false.

Here's how it should look:

public ICommand RelayCommand_MoveUp
{
  get
  {
    if (_relayCommand_MoveUp == null)
    {
      _relayCommand_MoveUp = new RelayCommand(Execute_MoveUp, CanExecute_MoveUp);

    }
    return _relayCommand_MoveUp;
  }
}

private void Execute_MoveUp(object sender)
{
  if (_selectedFolder != null)
  {
    _selectedFolder.SelectParent();
  }
}

private void CanExecute_MoveUp(object sender)
{
  return (_selectedFolder != null) && (_selectedFolder.Parent != null);
}



EDIT (Added from discussion in comments):

If you want to use something more like the RoutedCommands, which will make the ViewModels more dependent on WPF specific views, there are some good options available.

This discussion got the whole idea of using RoutedCommands in conjunction with MVVM started.

And here's a very solid solution to the issues presented by Josh Smith and Bill Kempf.

rmoore
Thanks, rmoore. So, do I understand correctly that if I need to access the EventArgs within the called method I cannot use the RelayCommand class to call it? +tom
Tom A
That's correct, the basic ICommand doesn't implement any events, in fact neither to my knowledge does the RoutedCommand, those actually come from something called a CommandBinding, which is what the RoutedCommand looks for.I added some addtional info for RoutedCommands in MVVM to my post, as there's not enough room in this comment =)
rmoore
Thanks much! +10...
Tom A
A: 

This weekend (August 22) Josh Smith checked in new changes into codeplex for his MvvmFoundation project which changes the way RelayCommand works for delegates with a parameter. Beware!

To pass a parameter to the delegate, you'll need to use his new RelayCommand<T> constructor instead:

 public ICommand GotoRegionCommand
 {
  get
  {
   if (_gotoRegionCommand == null)
    _gotoRegionCommand = new RelayCommand<String>(GotoRegionCommandWithParameter);
   return _gotoRegionCommand;
  }
 }
 private void GotoRegionCommandWithParameter(object param)
 {
  var str = param as string;
 }
JasonD
Thanks for the info, jasonD. I have abandoned the Josh Smith code in favor of the new MS MVVM Toolkit 0.1 Visual Studio project template, which I am *very* pleased with. It is easy to use and has very good documentation. See http://wpf.codeplex.com/Wiki/View.aspx?title=WPF%20Model-View-ViewModel%20Toolkit .
Tom A