Public Class DelegateCommand
Implements ICommand
Private _executeAction As Action(Of Object)
Private _canExecuteCache As Boolean
Private _canExecute As Func(Of Object, Boolean)
''' <summary>
''' Initializes a new instance of the <see cref="DelegateCommand"/> class.
''' </summary>
''' <param name="executeAction">The execute action.</param>
''' <remarks></remarks>
Public Sub New(ByVal executeAction As Action(Of Object))
Initialize(executeAction,
Function()
Return True
End Function)
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="DelegateCommand"/> class.
''' </summary>
''' <param name="executeAction">The execute action.</param>
''' <param name="canExecute">Whether it can execute.</param>
Public Sub New(ByVal executeAction As Action(Of Object), ByVal canExecute As Func(Of Object, Boolean))
Initialize(executeAction, canExecute)
End Sub
Public Sub Initialize(ByVal executeAction As Action(Of Object), ByVal canExecute As Func(Of Object, Boolean))
_executeAction = executeAction
_canExecute = canExecute
End Sub
''' <summary>
''' Determines whether the command can execute in its current state.
''' </summary>
''' <param name="parameter">Data used by the command.</param>
''' <returns>True if the command can be executed; False otherwise.</returns>
Public Function CanExecute(ByVal parameter As Object) As Boolean _
Implements ICommand.CanExecute
Dim temp = _canExecute(parameter)
If _canExecuteCache <> temp Then
_canExecuteCache = temp
RaiseEvent CanExecuteChanged(Me, New EventArgs)
End If
Return _canExecuteCache
End Function
Public Sub RaiseCanExecuteChanged()
OnCanExecuteChanged(EventArgs.Empty)
End Sub
Protected Overridable Sub OnCanExecuteChanged(ByVal e As EventArgs)
RaiseEvent CanExecuteChanged(Me, e)
End Sub
''' <summary>
''' Occurs when changes happen that affects whether the command should execute.
''' </summary>
Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Implements ICommand.CanExecuteChanged
''' <summary>
''' Defines method to call when the command is invoked.
''' </summary>
''' <param name="parameter">Data used by the command.</param>
Public Sub Execute(ByVal parameter As Object) _
Implements ICommand.Execute
_executeAction(parameter)
End Sub
End Class