我是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 < object>'的lambda表达式中的值。
最后,在实际的类中,有两个属性需要引用,而我似乎无法在互联网上找到它们:
if (_myCommand == null)
{
_myCommand = new RelayCommand(p => this.DoMyCommand(p),
p => this.CanDoMyCommand(p));
}DoMyCommand和CanDoMyCommand错误地说:
'RelayCommand‘不包含'DoMyCommand’的定义,也找不到接受'RelayCommand‘类型的第一个参数的扩展方法'DoMyCommand’(您缺少使用指令还是程序集引用?)。
EDIT1:
这是我的另一个代码:
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发布于 2014-01-03 09:17:47
尝尝这个
AddQuestionCommand = new RelayCommand(o =>
{
var title = this.Items.OfType<MyTitleModel>().LastOrDefault();
Items.Add(new MyQuestionModel() { Title = title });
},(p)=>Items.Any()); 第一个参数应该是Action,第二个是返回Bool的Func。而在您的上述代码中,则相反。
更新
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
}发布于 2014-01-03 09:21:51
试试relayCommand的这个实现
公共类RelayCommand : ICommand { #region字段
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
}私有bool CanSave() { // }
https://stackoverflow.com/questions/20900158
复制相似问题