Delegate Command in VB.NET

Following code snippet contains the implementation of ICommand interface. The code below shows a generic implementation of ICommand interface.


Code:

Namespace Commands
    Public Class DelegateCommand(Of T)
        Implements ICommand
 
        Private executeMethod As Action(Of T)
        Private canExecuteMethod As Func(Of T, Boolean)
 
        ''' 
        ''' Occurs when changes occur that affect whether or not the command should execute.
        ''' 
        ''' 
        Public Event CanExecuteChanged As EventHandler Implements System.Windows.Input.ICommand.CanExecuteChanged
 
        ''' 
        ''' Initializes a new instance of  class.
        ''' 
        ''' Method that execute when command is invoked.
        Public Sub New(ByVal executeMethod As Action(Of T))
            MyClass.New(executeMethod, Nothing)
        End Sub
 
        ''' 
        ''' Initializes a new instance of  class.
        ''' 
        ''' Method that execute when command is invoked.
        ''' Method that determines whether the command can execute in its current state.
        Public Sub New(ByVal executeMethod As Action(Of T), ByVal canExecuteMethod As Func(Of T, Boolean))
            If executeMethod Is Nothing AndAlso canExecuteMethod Is Nothing Then
                Throw New ArgumentNullException("executeMethod")
            End If
            Me.executeMethod = executeMethod
            Me.canExecuteMethod = canExecuteMethod
        End Sub
 
        ''' 
        '''  Determines whether the command can execute in its current state.
        ''' 
        ''' Data used by the command. If the command does not require data to be passed, this object can be set to null.
        ''' True if this command can be executed; otherwise, False.
        Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
            If canExecuteMethod Is Nothing Then
                Return True
            End If
            If parameter Is Nothing Then
                Return canExecuteMethod(Nothing)
            End If
            Return canExecuteMethod(CType(parameter, T))
        End Function
 
        ''' 
        ''' Called when the command is invoked.
        ''' 
        ''' Data used by the command. If the command does not require data to be passed, this object can be set to null.
        Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
            If executeMethod Is Nothing Then
                Return
            End If
            If parameter Is Nothing Then
                executeMethod(Nothing)
            Else
                executeMethod(CType(parameter, T))
            End If
        End Sub
 
        ''' 
        ''' Raises the CanExecuteChanged event.
        ''' 
        Public Sub RaiseCanExecuteChanged()
            RaiseEvent CanExecuteChanged()
        End Sub

    End Class
End Namespace

1 comments :

Thanks for this post
I have used this code in my project

but thing to complete this for canExecuteMethod

please add this code for canExecuteMethod to


Public Sub RaiseCanExecuteChanged()
RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
End Sub

when you need that UI may eanble the call commandObject.RaiseCanExecuteChange from you viewModel


Emoticon Emoticon