【问题标题】:Command Parameter with RelayCommand使用 RelayCommand 的命令参数
【发布时间】:2014-03-30 01:42:45
【问题描述】:

我正在使用通用 RelayCommand 类的版本并尝试传递命令参数但没有成功。我收到以下错误:

Error 29 Method 'Private Sub KeyPadPressExecute(param As Object)' does not have a signature compatible with delegate 'Delegate Sub Action()'.

我查看了许多在命令中使用参数的示例,但我无法理解它们。我不明白我应该从哪里获取参数,该参数将被传递给KeyPadPressExecute()

以下是相关代码:

Private Sub KeyPadPressExecute(param As Object)
    Debug.Print(param.ToString)
End Sub
Private Function CanKeyPadPressExecute() As Boolean
    Return True
End Function
Public ReadOnly Property KeyPadPress() As ICommand
    Get 'Error occurs on next line
        Return New RelayCommand(AddressOf KeyPadPressExecute, AddressOf CanKeyPadPressExecute)
    End Get
End Property

我的 XAML 是什么样子的:

<Button Command="{Binding KeyPadPress}" CommandParameter="1" Width="84"></Button>

我正在使用 VS 2012 并针对 .NET 4.5。

注意:我原来的帖子说我使用的是 MicroMVVM。对该项目的仔细检查表明,名为 MicroMVVM 的程序集与托管在 codeplex 的 MicroMVVM 框架无关。这个项目是别人发起的,所以很混乱。

【问题讨论】:

  • 我将代码更改为Return New RelayCommand(Of String)(AddressOf KeyPadPressExecute...,现在可以正常使用了。

标签: wpf vb.net mvvm command .net-4.5


【解决方案1】:

GitHub 看到的RelayCommand 实现中,可以看出该类有效地隐藏和吞下了命令参数。也许您应该尝试使用该类的通用版本:RelayCommand(Of T)。运行时异常非常明显:MicroMVVM 类的构造函数之间存在不匹配,仅采用 blunt Sub Action() 委托;以及您的命令的执行处理程序,采用 object 参数。

NB:请原谅我的不好VB,这不是我的母语

更新:我看到 MicroMVVM 托管在 CodePlex,但那里没有 RelayCommand 类(显然被更智能的 DelegateCommand 类取代)。 GitHub 上可能不相关的项目的链接仍然用作示例和信息源。欢迎评论。

【讨论】:

    猜你喜欢
    • 2012-01-05
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2015-11-02
    • 2018-04-08
    • 2019-03-02
    相关资源
    最近更新 更多