C#> VB转换,RelayCommand的行为不同

时间:2010-04-19 09:54:07

标签: c# vb.net

    [Visual C#]
    public ICommand MyCommand
    {
        get
        {
            if (this.myCommand == null)
            {
                this.myCommand = new RelayCommand(this.ShowMyCommand);
            }

            return this.myCommand;
        }
    }

    private void ShowMyCommand(object param)
    {
        ...
    }

此代码工作正常,但当我将其转换为Visual Basic时:

[Visual Basic]
Private _myCommand As RelayCommand
Public ReadOnly Property MyCommand As ICommand
    Get
        If Me._myCommand Is Nothing Then
            Me._myCommand = New RelayCommand(Me.ShowMyCommand)
        End If

        Return Me._myCommand
    End Get
End Property

Private Sub ShowMyCommand(ByVal param As Object)

    ...

End Sub

我收到错误:

  

错误3未指定参数   'Private Sub'的参数'param'   ShowMyCommand(param As Object)'。

有什么想法吗?我只是盲目转换,所以我不明白该项目的作用,我只是转换它。

1 个答案:

答案 0 :(得分:5)

对于VB来说,我有点茫然,但根据我所知,你需要在方法名前加上关键字AddressOf,以便它可以用作方法组。事件。

以下一行:

Me._myCommand = New RelayCommand(Me.ShowMyCommand)

需要写成:

Me._myCommand = New RelayCommand(AddressOf Me.ShowMyCommand)

错误消息是因为编译器正在尝试编译对该方法的调用,因此缺少其参数的参数。