[英]Implementing RelayCommand (MVVM) in VB.NET: Syntax problems
对于一个项目,我必须使用VB.NET。 我将使用MVVM,因此必须实现RelayCommand
类。
在C#中,该类如下所示:
class ActionCommand : ICommand
{
private Action<object> execute;
private Predicate<object> canExecute;
private event EventHandler CanExecuteChangedInternal;
public ActionCommand(Action<object> execute) : this(execute, DefaultCanExecute) { }
public ActionCommand(Action<object> exec, Predicate<object> canExec)
{
if (exec == null)
throw new ArgumentNullException("execute");
if (canExec == null)
throw new ArgumentNullException("canExecute");
execute = exec;
canExecute = canExec;
}
public event EventHandler CanExecuteChanged
{
add { CanExecuteChangedInternal += value; }
remove { CanExecuteChangedInternal -= value; }
}
public bool CanExecute(object param)
{
return canExecute != null && canExecute(param);
}
public void Execute(object param)
{
execute(param);
}
public void OnCanExecuteChanged()
{
EventHandler handler = CanExecuteChangedInternal;
if (handler != null)
handler.Invoke(this, EventArgs.Empty);
}
public void Destroy()
{
canExecute = _ => false;
this.execute = _ => { return; };
}
private static bool DefaultCanExecute(object param)
{
return true;
}
}
我在VB.NET中的版本如下所示:
Public Class RelayCommand : Implements ICommand
Dim _execute As Action(Of Object)
Dim _canExecute As Predicate(Of Object)
Dim canExecuteChangedInternal As EventHandler
Sub New(ByVal execute)
Me.New(execute, DefaultCanExecute)
End Sub
Sub New(ByVal execute As Action(Of Object), ByVal canExec As Predicate(Of Object))
If execute = Nothing Then
Throw New ArgumentException("execute")
End If
If canExec = Nothing Then
Throw New ArgumentException("canExec")
End If
_execute = execute
_canExecute = canExec
End Sub
Public Function CanExecute(ByRef param As Object) As Boolean
Return _canExecute <> Nothing And _canExecute(param)
End Function
Public Sub Execute(ByVal obj As Object)
_execute(obj)
End Sub
Public Custom Event CanExecuteChanged As EventHandler
AddHandler(ByVal value As EventHandler)
AddHandler canExecuteChangedInternal, AddressOf value
End AddHandler
RemoveHandler(value As EventHandler)
RemoveHandler canExecuteChangedInternal, AddressOf value
End RemoveHandler
RaiseEvent(sender As Object, e As EventArgs)
End RaiseEvent
End Event
Public Sub OnCanExecuteChanged(ByVal param As Object)
Dim handler As EventHandler = canExecuteChangedInternal
If handler <> Nothing Then
handler.Invoke(Me, EventArgs.Empty)
End If
End Sub
Public Sub Destroy()
_canExecute = Function(param As Object)
Return False
End Function
Me._execute = Sub()
Return
End Sub
End Sub
Private Shared Function DefaultCanExecute(ByVal param As Object) As Boolean
Return True
End Function
End Class
现在我在VB中存在以下问题:
DefaultCanExecute
不作为其他构造函数的参数接受,但是在C#中,它可以正常工作。 EventHandlers
无效。 错误消息是:“值不是方法的名称”。 但这是一个事件处理程序,不是吗? C#版本也可以在这里使用。 多亏了戴夫(Dave),我才能使所有事情都得到解决。 如果有人对课程的最终版本感兴趣,请访问:
Public Class RelayCommand : Implements ICommand
Private _execute As Action(Of Object)
Private _canExecute As Predicate(Of Object)
Private Event canExecuteChangedInternal As EventHandler
Public Sub New(ByVal execute As Object)
Me.New(execute, AddressOf DefaultCanExecute)
End Sub
Public Sub New(ByVal execute As Action(Of Object), ByVal canExec As Predicate(Of Object))
If execute Is Nothing Then
Throw New ArgumentException("execute")
End If
If canExec Is Nothing Then
Throw New ArgumentException("canExec")
End If
_execute = execute
_canExecute = canExec
End Sub
Public Function CanExecute(ByVal param As Object) As Boolean Implements ICommand.CanExecute
Return _canExecute IsNot Nothing AndAlso _canExecute(param)
End Function
Public Sub Execute(ByVal obj As Object) Implements ICommand.Execute
_execute(obj)
End Sub
Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
AddHandler(ByVal value As EventHandler)
AddHandler canExecuteChangedInternal, value
End AddHandler
RemoveHandler(value As EventHandler)
RemoveHandler canExecuteChangedInternal, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent canExecuteChangedInternal(sender, e)
End RaiseEvent
End Event
Public Sub OnCanExecuteChanged(ByVal param As Object)
Dim handler As EventHandler = canExecuteChangedInternalEvent
If handler IsNot Nothing Then
handler.Invoke(Me, EventArgs.Empty)
End If
End Sub
Public Sub Destroy()
_canExecute = Function(unused) False
Me._execute = Sub(unused)
Return
End Sub
End Sub
Private Shared Function DefaultCanExecute(ByVal param As Object) As Boolean
Return True
End Function
End Class
关于转换,有很多奇怪的地方:1.对于引用类型,您需要使用“ Is”和“ IsNot”。 2.您省略了第一个构造函数的类型。 3.您在“销毁”方法中省略了第二个lambda的参数。
请尝试以下操作:
Friend Class ActionCommand
Implements ICommand
Private _execute As Action(Of Object)
Private _canExecute As Predicate(Of Object)
Private Event CanExecuteChangedInternal As EventHandler
Public Sub New(ByVal execute As Action(Of Object))
Me.New(execute, AddressOf DefaultCanExecute)
End Sub
Public Sub New(ByVal exec As Action(Of Object), ByVal canExec As Predicate(Of Object))
If exec Is Nothing Then
Throw New ArgumentNullException("execute")
End If
If canExec Is Nothing Then
Throw New ArgumentNullException("canExecute")
End If
_execute = exec
_canExecute = canExec
End Sub
Public Custom Event CanExecuteChanged As EventHandler
AddHandler(ByVal value As EventHandler)
AddHandler CanExecuteChangedInternal, value
End AddHandler
RemoveHandler(ByVal value As EventHandler)
RemoveHandler CanExecuteChangedInternal, value
End RemoveHandler
RaiseEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
RaiseEvent CanExecuteChangedInternal(sender, e)
End RaiseEvent
End Event
Public Function CanExecute(ByVal param As Object) As Boolean
Return _canExecute IsNot Nothing AndAlso canExecute(param)
End Function
Public Sub Execute(ByVal param As Object)
execute(param)
End Sub
Public Sub OnCanExecuteChanged()
'referencing the hidden "Event" field of the CanExecuteChangedInternal event:
Dim handler As EventHandler = CanExecuteChangedInternalEvent
If handler IsNot Nothing Then
handler.Invoke(Me, EventArgs.Empty)
End If
End Sub
Public Sub Destroy()
_canExecute = Function(underscore) False
Me._execute = Sub(underscore)
Return
End Sub
End Sub
Private Shared Function DefaultCanExecute(ByVal param As Object) As Boolean
Return True
End Function
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.