无法将单个参数传递给 MVVM Light Toolkit 的 RelayCommand 中的 lambda 函数
我不知道 Josh Smith 和 Laurent Bugnion 的 RelayCommand 实现之间是否存在差异,但在我看过的所有地方,听起来 RelayCommand 的执行部分可以采用 0 或 1 个参数。我只能让它与 0 一起工作。当我尝试类似以下操作时,
public class Test
{
public RelayCommand MyCommand { get; set; }
public Test()
{
MyCommand = new RelayCommand((param) => SomeFunc(param));
}
private void SomeFunc( object param)
{
}
}
我收到错误:Delegate 'System.Action' does not take '1' argument
。为了确保我没有疯,我查看了 RelayCommand 的定义,以确保我的解决方案中的某个地方没有一些流氓实现,但可以肯定的是,这只是 Action,而不是 Action<>。
我到底错过了什么?
I don't know if there's a difference between Josh Smith's and Laurent Bugnion's implementations of RelayCommand or not, but everywhere I've looked, it sounds like the Execute portion of RelayCommand can take 0 or 1 parameters. I've only been able to get it to work with 0. When I try something like:
public class Test
{
public RelayCommand MyCommand { get; set; }
public Test()
{
MyCommand = new RelayCommand((param) => SomeFunc(param));
}
private void SomeFunc( object param)
{
}
}
I get the error: Delegate 'System.Action' does not take '1' arguments
. Just to make sure I am not insane, I went to the definition of RelayCommand to make sure I didn't have some rogue implementation in my solution somewhere, but sure enough, it was just Action, and not Action<>.
What on earth am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RelayCommand
的非通用实现(在 MVVM Light 中)不接受参数。请改用RelayCommand
The non-generic implementation of
RelayCommand
(in MVVM Light) does not accept a parameter. UseRelayCommand<Object>
instead, or (even better)RelayCommand<YourCustomType>
so the parameter toSomeFunc
is strongly typed.