It should be tip top after you substitute your Command
class to the code below
public class Command : ICommand
{ //private readonly Action<object> _execute;
private readonly Action _execute;
//private readonly Func<object, bool> _canExecute;
private readonly Func<bool> _canExecute;//instead of prev line
//public Command(Action<object> execute)
public Command(Action execute)//instead of prev line
: this(execute, null)
{ }
//public Command(Action<object> execute,
public Command(Action execute,//instead of prev line
Func<bool> canExecute)//added instead of next line
//Func<object, bool> canExecute)
{ _execute = execute;
_canExecute = canExecute;
}
public void Execute(object parameter)
{
//_execute(parameter);
_execute();//added instead of prev line
}
public bool CanExecute(object parameter)
{ return (_canExecute == null)
//|| _canExecute(parameter);
|| _canExecute();//added instead of prev line
}
public event EventHandler CanExecuteChanged = delegate { };
public void RaiseCanExecuteChanged()
{ CanExecuteChanged(this, new EventArgs()); }
}
Checked this on the code from the mentioned book (p.205, Chapter 5)
- Vice R., Shujaat Siddiqi M. - MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF, PACKT Publishing, ISBN 978-1-84968-342-5, 2012
This is the error/typo in text of book
Really, in the accompanying code to this book which anyone can get from Download the support files for this book, the correct RelayCommand
class is used instead of Command
class in the text of the book, according to which I made changes above to Command
class
BTW, it is absent in online errata to the book
Changing to IsCustomerSelected()
method to IsCustomerSelected(object param)
will bring
on ShowCustomerDetails
in following snippet (it is after "The method call stuff:" in question):
private Command _showDetailsCommand;
public Command ShowDetailsCommand
{
get
{
return _showDetailsCommand ??
(_showDetailsCommand =
new Command
(
ShowCustomerDetails,//error
IsCustomerSelected
)
);
}
}
the error:
Expected a method with 'void ShowCustomerDetails(object)' signature
since ShowCustomerDetails()
is:
public void ShowCustomerDetails()
{
if (!IsCustomerSelected())
throw new InvalidOperationException("Unable to display customer details. "
+ "No customer selected.");
}
and if to change the latter ShowCustomerDetails(object param)
this brings more changes and more necessities to change the code with each sequential change.
Just run the code and try incorporate your change to see what it would invoke