Need help understanding MVVM tutorial, RelayCommand

I am reading a http://msdn.microsoft.com/en-us/magazine/dd419663.aspx tutorial

I do not understand what the following code is trying to do.

_saveCommand = new RelayCommand(param => this.Save(), param => this.CanSave ); 

As defined in the Realy Command class, CanSave had to be a method with a parameter, because it maps to a predicate, so the correction method must have the same parameter as for the action object. Please help to understand.

+3
source share
2 answers

RelayCommanduses functions (more precisely, delegates) passed to its constructor to implement methods CanExecuteand Execute.

. , - Save RelayCommand . , , - CanSave.

, Command.

UPD:

, : Save() Action, Action, Save() . - .

, .

 _saveCommand = new RelayCommand(param => this.Save(), param => this.CanSave ); 

( # v2.0)

 _saveCommand = new RelayCommand(
     new Action<object>(delegate(object param){ this.Save(); }), 
     new Func<object,bool>(delegate(object param){ return this.CanSave; })); 

, , , .

, :

 // it is OK to ignore methods arguments. 
 // So, it also OK to ignore them in anonymous methods as well
 private void Save_Anonymous(object parameter){
     this.Save();
 }
 private bool CanSave_Anonymous(object parameter){
     return this.CanSave;
 }

 ....

 _saveCommand = new RelayCommand(new Action<object>(this.Save_Anonymous), 
        new Func<object, bool>(this.CanSave_Anonymous));

, , , . . , , , .

+2

-, RelayCommand WPF. WPFlight, . WPOM ICommand : . , , , . , .

, .

1

.

class demoViewModel
{
    string filename = "";
    private ICommand _saveCommand;
    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand== null)
            {
                _saveCommand = new RelayCommand<object>(
                     action => Save(filename),
                     predicate => CanSave(filename));

            }
            return _saveCommand;
        }
    }

    private bool CanSave(string fname)
    {
        return (!string.IsNullOrEmpty(fname));
    }

    private void Save(string fname)
    {
        SaveHelper(fname);//some logic goes inside SaveHelper
    }        
}

2

. .

class demoViewModel1
{
    string filename = "";
    private ICommand _saveCommand;
    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand== null)
            {
                _saveCommand = new RelayCommand<object>(
                     action => { SaveHelper(filename); },
                     predicate => { return (!string.IsNullOrEmpty(filename)); }
                     );

            }
            return _saveCommand;
        }
    }
}

3

-, ,

class demoViewModel2
{
    string filename = "";
    private ICommand _saveCommand;
    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand== null)
            {
                _saveCommand = new RelayCommand<object>(
                     (objParamForAction) => { SaveHelper(filename); },
                     () => { return (!string.IsNullOrEmpty(filename)); }
                     );

            }
            return _saveCommand;
        }
    }
}
+2

All Articles