问题描述
我正在根据Josh Smith应用MVVM模式,并且遇到了困难.我一直在这里研究问题,似乎语法不太正确.
I am applying the MVVM pattern per Josh Smith and having difficulty. I've been researching the problem here and can't seem to get the syntax quite right.
在我看来,以下代码遵循必需的语法,但是Visual Studio报告错误在指示的行上删除'System.Action'不接受'2'参数" .
The code below looks to me like it follows the required syntax, but Visual Studio reports error "Delegate 'System.Action' does not take '2' arguments" on the line indicated.
有人可以看到我在哪里犯错吗?谢谢!
+ tom
Can someone see where I am making a mistake? Thanks!
+tom
RelayCommand _relayCommand_MoveUp;
public ICommand RelayCommand_MoveUp
{
get
{
if (_relayCommand_MoveUp == null)
{
_relayCommand_MoveUp = new RelayCommand(
(sender, e) => this.Execute_MoveUp(sender, e), **ERROR REPORTED HERE**
(sender, e) => this.CanExecute_MoveUp(sender, e));
return _relayCommand_MoveUp;
}
}
}
private void Execute_MoveUp(object sender, ExecutedRoutedEventArgs e)
{
if (_selectedFolder != null)
{
_selectedFolder.SelectParent();
}
}
private void CanExecute_MoveUp(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = (_selectedFolder != null) && (_selectedFolder.Parent != null);
}
//And from Josh Smith:
public class RelayCommand : ICommand
{
public RelayCommand(Action<object> execute);
public RelayCommand(Action<object> execute, Predicate<object> canExecute);
public event EventHandler CanExecuteChanged;
[DebuggerStepThrough]
public bool CanExecute(object parameter);
public void Execute(object parameter);
}
推荐答案
RelayCommand不是RoutedCommand,我认为这是您最终感到困惑的地方.
The RelayCommand isn't a RoutedCommand, which I think is where you ending up confused.
Relay命令的构造函数采用动作委托,可选的谓词委托.这些委托不使用EventArgs,而仅使用单个Object参数,这就是为什么您遇到错误的原因.谓词还需要返回布尔类型,这是您将遇到的下一个错误.在CanExecute谓词中,而不是像设置RoutedCommand那样设置e.CanExecute,只需返回true/false.
The constructors for the Relay command take an Action delegate and optional Predicate delegate. These delegates don't take an EventArgs, just the single Object parameter, which is why you are encountering an error. The predicate also requires a return type of bool, which is the next error you'll get. In the CanExecute predicate instead of setting e.CanExecute like you do with a RoutedCommand you simply return true/false.
这是它的外观:
public ICommand RelayCommand_MoveUp
{
get
{
if (_relayCommand_MoveUp == null)
{
_relayCommand_MoveUp = new RelayCommand(Execute_MoveUp, CanExecute_MoveUp);
}
return _relayCommand_MoveUp;
}
}
private void Execute_MoveUp(object sender)
{
if (_selectedFolder != null)
{
_selectedFolder.SelectParent();
}
}
private void CanExecute_MoveUp(object sender)
{
return (_selectedFolder != null) && (_selectedFolder.Parent != null);
}
编辑(从评论的讨论中添加):
EDIT (Added from discussion in comments):
如果您想使用诸如RoutedCommands之类的东西,这将使ViewModels更依赖于WPF特定的视图,则有一些不错的选择.
If you want to use something more like the RoutedCommands, which will make the ViewModels more dependent on WPF specific views, there are some good options available.
此讨论得到了全部将RoutedCommands与MVVM结合使用的想法开始了.
This discussion got the whole idea of using RoutedCommands in conjunction with MVVM started.
并且这是一个非常可靠的解决方案,用于解决乔什·史密斯(Josh Smith)提出的问题和比尔·肯普夫(Bill Kempf).
And here's a very solid solution to the issues presented by Josh Smith and Bill Kempf.
这篇关于RelayCommand Lambda语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!