RelayCommand类问题

时间:2014-01-03 09:14:28

标签: c# wpf lambda

我是MVVM的新手。我已经创建了一个RelayCommand类,但是我在lambda表达式中遇到了一些错误。

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

这行代码返回错误说:

只能将赋值,调用,递增,递减,等待和新对象表达式用作语句

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

这行代码返回错误说:

并非所有代码路径都返回类型为'System.Predicate&lt;的lambda表达式中的值对象&gt;”

最后,在实际的课程中有2个属性需要参考,我在互联网上似乎无法找到它们:

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

DoMyCommandCanDoMyCommand有错误说明:

'RelayCommand'不包含'DoMyCommand'的定义,并且没有扩展方法'DoMyCommand'接受类型'RelayCommand'的第一个参数可以找到(你是否缺少using指令或汇编引用?< / strong>

EDIT1:

Binson Eldhose这是我的另一段代码:

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

EDIT2:

以下是RelayCommand构造函数:

#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

2 个答案:

答案 0 :(得分:2)

试试这个

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

第一个参数应该是Action,第二个Func返回Bool。在上面的代码中反向

  

更新

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

更新

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
    }

答案 1 :(得分:0)

尝试使用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 }
}

用法

private RelayCommand _saveCommand;

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


    private void Save()
    {

     // Saving code


    }

私人布尔CanSave()         {            //         }