如何在VB.net中编写DelegateCommand函数参数

时间:2014-01-08 12:11:41

标签: c# .net wpf vb.net mvvm

我正在用C#学习WPF - MVVM项目教程,我也试图用VB编写它。

在ViewModel文件(C#)中,代码如下:

    public ICommand RemoveBookCommand
    {
        get
        {
            if (_removeBookCommand == null)
            {
                _removeBookCommand = new DelegateCommand<Book>(RemoveBook, CanRemoveBook);
            }
            return _removeBookCommand;
        }
    }

    private bool CanRemoveBook(Book book)
    {
        return Books.Count > 0 && Books.Contains(book);
    }

    private void RemoveBook(Book book)
    {
        Books.Remove(book);
        CurrentBook = null;
    }

我将它转换为VB,如下所示:

    Public Property RemoveBookCommand() As ICommand
        Get
            If _removeBookCommand Is Nothing Then
                _removeBookCommand = New DelegateCommand(Of Book)(RemoveBook, CanRemoveBook)
            End If
            Return _removeBookCommand
        End Get
        Set(value As ICommand)

        End Set
    End Property

    Private Function CanRemoveBook(book As Book) As Boolean
        Return Books.Count > 0 And Books.Contains(book)
    End Function

    Private Sub RemoveBook(book As Book)
        Books.Remove(book)
        CurrentBook = Nothing
    End Sub

似乎没有问题

但Visual Studio说:

  

未指定参数'book'的'Private Function CanRemoveBook(book As Models.Book)As Boolean'。

     

未为'Private Sub RemoveBook(book As Models.Book)'的参数'book'指定参数。

当没有为函数指定'Book'参数时,它运行C#。那么,VB.net中的问题是什么

1 个答案:

答案 0 :(得分:2)

正确的语法是:

New DelegateCommand(Of Book)(AddressOf RemoveBook, AddressOf CanRemoveBook)

请参阅http://msdn.microsoft.com/de-de/library/y72ewk2b(v=vs.90).aspx