Search code examples
wpfvb.netxamlroutedcommand

Unable to implement ICommandSource in VB


Has anyone ever tried to implement ICommandSource with VB? The examples provided by Microsoft are in C#, and since VB doesn't allow implicit implementation this interface is like inachievable in VB!!

http://msdn.microsoft.com/en-us/library/ms771361.aspx


Solution

  • It depends on what class you want to implement it on. If you're introducing the Command, CommandParameter and CommandTarget properties in your own class (where you're implementing the interface), you can just implement it like any other interface:

    Public ReadOnly Property Command() As ICommand
      Implements ICommandSource.Command
      Get
        ' implementation goes here
      End Get
    End Property
    

    You can still use a DP for the implementation, by the way: the Implements directive is on the CLR property and does not interfere with the "do not touch" implementation of the getters and setters.

    If the class you want to implement it on already has (inherited) Command, CommandParameter and CommandTarget properties, and you want the interface implementation to reuse those, you'll need to create new properties with new names, declare them as interface implementations and back them onto the existing properties

    Public ReadOnly Property ICommandSource_Command() As ICommand
      Implements ICommandSource.Command
      Get
        Return Me.Command  ' the existing implementation that can't be made implicit
      End Get
    End Property