gpt4 book ai didi

mvvm - RelayCommand lambda 语法问题

转载 作者:行者123 更新时间:2023-12-03 10:53:59 24 4
gpt4 key购买 nike

我正在应用 Josh Smith 的 MVVM 模式并且遇到了困难。我一直在这里研究这个问题,但似乎无法完全正确地使用语法。

在我看来,下面的代码符合所需的语法,但 Visual Studio 报告错误 “委托(delegate)‘System.Action’不接受‘2’参数”在指示的行上。

有人可以看到我在哪里犯了错误吗?谢谢!
+汤姆

    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);
}

最佳答案

本周末(8 月 22 日)Josh Smith 为他的 MvvmFoundation 项目在 codeplex 中 checkin 了新的更改,该项目更改了 RelayCommand 为具有参数的代表工作的方式。谨防!

要将参数传递给委托(delegate),您需要使用他的新 RelayCommand 构造函数:

    public ICommand GotoRegionCommand
{
get
{
if (_gotoRegionCommand == null)
_gotoRegionCommand = new RelayCommand<String>(GotoRegionCommandWithParameter);
return _gotoRegionCommand;
}
}
private void GotoRegionCommandWithParameter(object param)
{
var str = param as string;
}

关于mvvm - RelayCommand lambda 语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/900437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com