Ben Clarke
Ben Clarke

Reputation: 256

RelayCommand Class issue

I am new to MVVM. I have made a RelayCommand class but I'm getting some errors with the lambda expression.

AddTitleCommand = new RelayCommand(o => true, o => Items.Add(new MyTitleModel()));

This line of code is returning an error saying:

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

AddQuestionCommand = new RelayCommand(o => Items.Any(), o =>
{
            var title = this.Items.OfType<MyTitleModel>().LastOrDefault();
            Items.Add(new MyQuestionModel() { Title = title });
}); 

This line of code is returning the error saying:

Not all code paths return a value in lambda expression of type 'System.Predicate < object>'

Finally, in the actual class there are 2 properties that need a reference which i cannot seem to find on the internet they are:

if (_myCommand == null)
{
            _myCommand = new RelayCommand(p => this.DoMyCommand(p),
                p => this.CanDoMyCommand(p));
}

The DoMyCommand and CanDoMyCommand have the error saying:

'RelayCommand' does not contain a definition for 'DoMyCommand' and no extension method 'DoMyCommand' accepting a first argument of type 'RelayCommand' could be found (are you missing a using directive or an assembly reference?

EDIT1:

Binson Eldhose This is my other piece of code:

RelayCommand _myCommand;
public ICommand MyCommand
{
    get
    {
        if (_myCommand == null)
        {
            _myCommand = new RelayCommand(p => this.DoMyCommand(p),
                p => this.CanDoMyCommand(p));
        }
        return _myCommand;
    }
}

EDIT2:

Here are the RelayCommand Constructors:

#region Constructors

public RelayCommand(Action<object> execute)
    : this(execute, null)
{
}

public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
    if (execute == null) throw new ArgumentException("execute");
    _execute = execute;
    _canExecute = canExecute;
}
#endregion // Constructors

Upvotes: 1

Views: 2236

Answers (2)

yo chauhan
yo chauhan

Reputation: 12295

Try this

AddQuestionCommand = new RelayCommand(o =>
{
        var title = this.Items.OfType<MyTitleModel>().LastOrDefault();
        Items.Add(new MyQuestionModel() { Title = title });
 },(p)=>Items.Any()); 

First parameter should be Action and second Func that return Bool . And in your above code its reverse

Update

AddTitleCommand = new RelayCommand( o => Items.Add(new MyTitleModel()),p => true));

Update

RelayCommand _myCommand;
public ICommand MyCommand
{
get
{
    if (_myCommand == null)
    {
        _myCommand = new RelayCommand(DoMyCommand,CanDoMyCommand);
    }
    return _myCommand;
}
}

bool CanDoMyCommand(object obj)
    {
        return true;//return true or false accordingly.
    }

    void DoMyCommand(object obj)
    { 
        //Do your work that you want to do on when Command Fires
    }

Upvotes: 2

Binson Eldhose
Binson Eldhose

Reputation: 749

Try this implementation of relayCommand

public class RelayCommand : ICommand { #region Fields

    private readonly Action<object> _execute;
    private readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null) throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion // Constructors

    #region ICommand Members [DebuggerStepThrough]

    public bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members }
}

Usage

private RelayCommand _saveCommand;

 public ICommand SaveCommand
        {
            get { return _saveCommand ?? (_saveCommand = new RelayCommand(p => Save(), p => CanSave())); }
        }


    private void Save()
    {

     // Saving code


    }

private bool CanSave() { // }

Upvotes: 0

Related Questions