C# ActionCommand:ICommand 到 VB.net 的翻译
我找到了一个 C# 类 ActionCommand,它实现了 ICommand 并基于 Execute 和 CanExecute 的委托。到目前为止对我来说看起来很完美。
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 有人可以帮忙翻译或解决这个问题吗?
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);
}
}
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
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
but this cannot satify ICommand.CanExecuteChanged
Can someone please help how to translate or solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您正在寻找的更像是:
I think what you are looking for is more like:
本文来自 MALIGUI .NET 博客 可能会有所帮助:
This article from MALIGUI .NET BLOG might help: