The action :
readonly Action _execute;
public RelayCommand(Action execute)
: this(execute, null)
{
}
public RelayCommand(Action execute, Func<Boolean> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
Other class's code:
public void CreateCommand()
{
RelayCommand command = new RelayCommand((param)=> RemoveReferenceExcecute(param));}
}
private void RemoveReferenceExcecute(object param)
{
ReferenceViewModel referenceViewModel = (ReferenceViewModel) param;
ReferenceCollection.Remove(referenceViewModel);
}
Why do I get the following exception, how can I fix it?
Delegate 'System.Action' does not take 1 arguments
System.Action
is a delegate for parameterless function. Use System.Action<T>
.
To fix this, replace your RelayAction
class with something like the following
class RelayAction<T> {
readonly Action<T> _execute;
public RelayCommand(Action<T> execute, Func<Boolean> canExecute){
//your code here
}
// the rest of the class definition
}
Note RelayAction
class should become generic. Another way is to directly specify the type of parameter _execute
will receive, but this way you'll be restricted in usage of your RelayAction
class. So, there are some tradeoff between flexibility and robustness.
Some MSDN links:
System.Action
System.Action<T>
You can define your command 'RemoveReferenceExcecute' without any parameters
RelayCommand command = new RelayCommand(RemoveReferenceExcecute);}
or you can pass some parameters / objects into it:
RelayCommand<object> command = new RelayCommand<object>((param)=> RemoveReferenceExcecute(param));}
In the second case do not forget to pass CommandParameter from your view;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With