C#的翻译ActionCommand:ICommand的成VB.net [英] Translation of C# ActionCommand:ICommand into VB.net

查看:217
本文介绍了C#的翻译ActionCommand:ICommand的成VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个C#类ActionCommand,实现了ICommand和依据代表的执行和CanExecute。 。看上去很适合我至今

I found a C# class ActionCommand, that implements ICommand and bases on delegates for Execute and CanExecute. Looks perfect for me so far.

  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变种(使用代码翻译和一些手)

Now I translated it into my needed VB.net variant (using code translators and some hands on)

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的事件。网上代码翻译建议如下:

My problem is around CanExecuteChanged and registering/mapping the events from CommandManager.RequerySuggested to CanExecuteChanged. The online code translator suggest the following:

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

但是这不能satify ICommand.CanExecuteChanged
是否有人可以帮助如何翻译或解决这个问题?

but this cannot satify ICommand.CanExecuteChanged Can someone please help how to translate or solve this?

推荐答案

本文从的 MALIGUI .NET博客可能有帮助:

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#的翻译ActionCommand:ICommand的成VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆