WPF命令类是否覆盖CanExecuteChanged-Event?

时间:2011-05-20 13:00:53

标签: wpf vb.net

目前我有以下Command类:

Public Class SubscribeCommand
    Implements ICommand

    Private ReadOnly _vm As MainWindowViewModel

    Public Sub New(ByVal vm As MainWindowViewModel)
        _vm = vm
    End Sub

    Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return Not String.IsNullOrEmpty(_vm.Symbol)
    End Function

    Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
        _vm.Subscribe()
    End Sub
End Class

在我阅读的教程中,您必须为canExecuteChanged-Event实现add {}和remove {}。但是我怎么能用vb.net做到这一点?

非常感谢..

2 个答案:

答案 0 :(得分:2)

如果你想在wpf中使用命令,请查看那里的wpf框架。你会发现至少两个很好的命令实现:

答案 1 :(得分:2)

我认为您不需要为CanExecuteChanged事件实现添加和删除部分。我很确定它现在的工作方式会很好。但是如果你确实想要出于某种原因(例如,为了使它与你在this post in C#,中看到的相匹配),你会改变

Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs)

Public Custom Event CanExecuteChanged As EventHandler

    AddHandler(ByVal value As EventHandler)

        CommandManager.RequestSuggested += value

    End AddHandler

    RemoveHandler(ByVal value As EventHandler)

           CommandManager.RequestSuggested -= value

    End RemoveHandler

End Event