我发现了一个C#类ActionCommand,该类实现ICommand并基于Execute和CanExecute的委托(delegate)。到目前为止,对我来说看起来很完美。

  public class ActionCommand : ICommand
  {
    private readonly Action<object> _executeHandler;
    private readonly Func<object, bool> _canExecuteHandler;

    public ActionCommand(Action<object> execute, Func<object, bool> canExecute)
    {
      if (execute == null)
        throw new ArgumentNullException("Execute cannot be null");
      _executeHandler = execute;
      _canExecuteHandler = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
    }
    public void Execute(object parameter)
    {
      _executeHandler(parameter);
    }
    public bool CanExecute(object parameter)
    {
      if (_canExecuteHandler == null)
        return true;
      return _canExecuteHandler(parameter);
    }
  }

现在,我将其翻译为所需的VB.net变体(使用代码转换器和一些动手工具)
Public Class ActionCommand
  Implements ICommand

  Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

  Private ReadOnly _executeHandler As Action(Of Object)
  Private ReadOnly _canExecuteHandler As Func(Of Object, Boolean)

  Public Sub New(ByVal execute As Action(Of Object),
                 ByVal canExecute As Func(Of Object, Boolean))
    If execute Is Nothing Then
      Throw New ArgumentNullException("Execute cannot be null")
    End If
    _executeHandler = execute
    _canExecuteHandler = canExecute
  End Sub

  Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
    _executeHandler(parameter)
  End Sub

  Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
    If (_canExecuteHandler Is Nothing) Then
      Return True
    End If
    Return _canExecuteHandler(parameter)
  End Function
End Class

我的问题是CanExecuteChanged和从CommandManager.RequerySuggested到CanExecuteChanged的事件注册/映射。在线代码翻译器建议以下内容:
Public Custom Event CanExecuteChanged As EventHandler
    AddHandler(ByVal value As EventHandler)
        CommandManager.RequerySuggested += value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
        CommandManager.RequerySuggested -= value
    End RemoveHandler
End Event

但这不能满足ICommand.CanExecuteChanged
有人可以帮忙翻译或解决此问题吗?

最佳答案

MALIGUI .NET BLOG的这篇文章可能会有所帮助:

Public Custom Event CanExecuteChanged As EventHandler _
    Implements ICommand.CanExecuteChanged
        AddHandler(ByVal value As EventHandler)
            Dim handler2 As EventHandler
            Dim canExecuteCommand = __CanExecuteCommand
            Do
                handler2 = canExecuteCommand
                Dim handler3 = DirectCast(System.Delegate.Combine(handler2, value), EventHandler)
                canExecuteCommand = Interlocked.CompareExchange((__CanExecuteCommand), handler3, handler2)
            Loop While (Not canExecuteCommand Is handler2)
            __CanExecuteCommand = canExecuteCommand
        End AddHandler
        RemoveHandler(ByVal value As EventHandler)
            Dim handler2 As EventHandler
            Dim canExecuteCommand = __CanExecuteCommand
            Do
                handler2 = canExecuteCommand
                Dim handler3 = DirectCast(System.Delegate.Remove(handler2, value), EventHandler)
                canExecuteCommand = Interlocked.CompareExchange((__CanExecuteCommand), handler3, handler2)
            Loop While (Not canExecuteCommand Is handler2)
            __CanExecuteCommand = canExecuteCommand
        End RemoveHandler
        RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
            If (__CanExecuteCommand IsNot Nothing) Then
                __CanExecuteCommand.Invoke(sender, e)
            End If
        End RaiseEvent
    End Event

关于c# - 将C#ActionCommand:ICommand转换为VB.net,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5484966/

10-09 03:26