gpt4 book ai didi

c# - C# ActionCommand :ICommand into VB.net 翻译

转载 作者:可可西里 更新时间:2023-11-01 08:04:34 24 4
gpt4 key购买 nike

我找到了一个 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有人可以帮忙翻译或解决这个问题吗?

最佳答案

Public Custom Event CanExecuteChanged As EventHandler
AddHandler(ByVal value As EventHandler)
AddHandler CommandManager.RequerySuggested, value
End AddHandler
RemoveHandler(ByVal value As EventHandler)
RemoveHandler CommandManager.RequerySuggested, value
End RemoveHandler
End Event

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com