无法将单个参数传递给 MVVM Light Toolkit 的 RelayCommand 中的 lambda 函数

发布于 2024-09-04 03:16:12 字数 582 浏览 0 评论 0原文

我不知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2024-09-11 03:16:12

RelayCommand 的非通用实现(在 MVVM Light 中)不接受参数。请改用 RelayCommand,或者(甚至更好)使用 RelayCommand,以便 SomeFunc 的参数是强类型的。

The non-generic implementation of RelayCommand (in MVVM Light) does not accept a parameter. Use RelayCommand<Object> instead, or (even better) RelayCommand<YourCustomType> so the parameter to SomeFunc is strongly typed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文