问题
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:
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.
回答2:
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;
}
来源:https://stackoverflow.com/questions/900437/relaycommand-lambda-syntax-problem