C# 到 VB.NET:在这种情况下应该使用 AddHandler 吗?
C# to VB.NET: Should AddHandler be used in this case?
我正在处理要转换为 VB.NET 的以下 C# 代码:
var interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent");
interactorAgent
.AddInteractorFor(currentWindowBounds)
.WithGazeAware()
.HasGaze(() => Console.WriteLine("Hey there!"))
.LostGaze(() => Console.WriteLine("Bye..."));
我想在出现“.HasGaze”或“.LostGaze”时获得函数回调。
我想我必须使用 "AddHandler" 而不是“=>”,但我不知道如何在 VB.NET 中做到这一点。
我应该在这里使用 AddHandler 是否正确?
我认为困难代码位于此声明中:
public static class InteractorExtensions
{
public static GazeAwareBehavior WithGazeAware(this IMutableBehaviorsInteractor interactor);
(...)
}
public class GazeAwareBehavior : EventHandlingBase, IBehavior, IChecksummable
{
public const string HasGazeChangedToken = "HazGazeChanged";
public GazeAwareBehavior();
public GazeAwareBehavior(GazeAwareMode mode = GazeAwareMode.Normal, TimeSpan? delayTime = null);
public TimeSpan DelayTime { get; set; }
public GazeAwareMode Mode { get; set; }
public event EventHandler<HasGazeChangedEventArgs> HasGazeChanged;
}
但我不确定。
这是更多的代码。
如果有人能阐明我需要做什么,我会很高兴。
谢谢。
public static void Main(string[] args)
{
// Everything starts with initializing Host, which manages the connection to the
// Tobii Engine and provides all the Tobii Core SDK functionality.
// NOTE: Make sure that Tobii.EyeX.exe is running
var host = new Host();
// InteractorAgents are defined per window, so we need a handle to it.
var currentWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
// Let's also obtain its bounds using Windows API calls (hidden in a helper method below).
var currentWindowBounds = GetWindowBounds(currentWindowHandle);
// Let's create the InteractorAgent.
var interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent");
// Next we are going to create an interactor, which we will define with the gaze aware behavior.
// Gaze aware behavior simply tells you whether somebody is looking at the interactor or not.
interactorAgent
.AddInteractorFor(currentWindowBounds)
.WithGazeAware()
.HasGaze(() => Console.WriteLine("Hey there!"))
.LostGaze(() => Console.WriteLine("Bye..."));
Console.ReadKey(true);
(...)
}
public static class InteractorExtensions
{
public static GazeAwareBehavior WithGazeAware(this IMutableBehaviorsInteractor interactor);
(...)
}
public class GazeAwareBehavior : EventHandlingBase, IBehavior, IChecksummable
{
public const string HasGazeChangedToken = "HazGazeChanged";
public GazeAwareBehavior();
public GazeAwareBehavior(GazeAwareMode mode = GazeAwareMode.Normal, TimeSpan? delayTime = null);
public TimeSpan DelayTime { get; set; }
public GazeAwareMode Mode { get; set; }
public event EventHandler<HasGazeChangedEventArgs> HasGazeChanged;
}
Public Class VirtualInteractorAgent(Of TInteractor As IInteractor, TData)
Inherits ProviderInteractorAgent(Of IInteractorRepository(Of TInteractor))
Protected Sub New(agentId As String, defaultWindowId As String, repository As IInteractorRepository(Of TInteractor), createInteractorDelegate As CreateInteractorDelegate)
Protected ReadOnly Property DefaultWindowId As String
Protected ReadOnly Property Repository As IInteractorRepository(Of TInteractor)
Public Sub RemoveInteractor(interactorId As String)
Public Sub RemoveInteractors(ParamArray ids() As String)
Public Sub Suspend()
Public Sub [Resume]()
Public Sub Clear()
Protected Overrides Sub Dispose(disposing As Boolean)
Public Function AddInteractorFor(data As TData) As TInteractor
Public Function AddInteractorFor(data As TData, Optional parentId As String = "_RootId", Optional z As Double = 0, Optional windowId As String = Nothing, Optional id As String = Nothing) As TInteractor
Public Function FindInteractor(interactorId As String) As TInteractor
Public Function AddInteractorsFor(ParamArray datas() As TData) As IEnumerable(Of TInteractor)
Public Function FindInteractors(ParamArray ids() As String) As IEnumerable(Of TInteractor)
Public Delegate Function CreateInteractorDelegate(data As TData, Optional parentId As String = "_RootId", Optional z As Double = 0, Optional windowId As String = Nothing, Optional id As String = Nothing) As TInteractor
End Class
每当您在 C# 代码中看到 =>
时,您就是在查看 Lambda 表达式。 C# 中 (params) => body
的等价物是 VB 中的 Function(params) body
或 Sub(params) body
,具体取决于 body
是否求值。在你的例子中,你的两个 Lambdas 都包含 Console.WriteLine
,所以两者都不计算为一个值,所以两者都应该是 Sub
:
Dim interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent")
interactorAgent.AddInteractorFor(currentWindowBounds).
WithGazeAware().
HasGaze(Sub() Console.WriteLine("Hey there!")).
LostGaze(Sub() Console.WriteLine("Bye..."));
一般也可以通过参数的类型判断是使用Function
还是Sub
。在这种情况下,HasGaze
和 LostGaze
可能都有一个类型为 Action
或类似的参数,这意味着使用 Sub
。如果它是 Func
或类似的,那么您将使用 Function
。 Action
是一个没有 return 值的委托,Func
是一个有 return 值的委托。
我正在处理要转换为 VB.NET 的以下 C# 代码:
var interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent");
interactorAgent
.AddInteractorFor(currentWindowBounds)
.WithGazeAware()
.HasGaze(() => Console.WriteLine("Hey there!"))
.LostGaze(() => Console.WriteLine("Bye..."));
我想在出现“.HasGaze”或“.LostGaze”时获得函数回调。
我想我必须使用 "AddHandler" 而不是“=>”,但我不知道如何在 VB.NET 中做到这一点。 我应该在这里使用 AddHandler 是否正确?
我认为困难代码位于此声明中:
public static class InteractorExtensions
{
public static GazeAwareBehavior WithGazeAware(this IMutableBehaviorsInteractor interactor);
(...)
}
public class GazeAwareBehavior : EventHandlingBase, IBehavior, IChecksummable
{
public const string HasGazeChangedToken = "HazGazeChanged";
public GazeAwareBehavior();
public GazeAwareBehavior(GazeAwareMode mode = GazeAwareMode.Normal, TimeSpan? delayTime = null);
public TimeSpan DelayTime { get; set; }
public GazeAwareMode Mode { get; set; }
public event EventHandler<HasGazeChangedEventArgs> HasGazeChanged;
}
但我不确定。
这是更多的代码。
如果有人能阐明我需要做什么,我会很高兴。
谢谢。
public static void Main(string[] args)
{
// Everything starts with initializing Host, which manages the connection to the
// Tobii Engine and provides all the Tobii Core SDK functionality.
// NOTE: Make sure that Tobii.EyeX.exe is running
var host = new Host();
// InteractorAgents are defined per window, so we need a handle to it.
var currentWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
// Let's also obtain its bounds using Windows API calls (hidden in a helper method below).
var currentWindowBounds = GetWindowBounds(currentWindowHandle);
// Let's create the InteractorAgent.
var interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent");
// Next we are going to create an interactor, which we will define with the gaze aware behavior.
// Gaze aware behavior simply tells you whether somebody is looking at the interactor or not.
interactorAgent
.AddInteractorFor(currentWindowBounds)
.WithGazeAware()
.HasGaze(() => Console.WriteLine("Hey there!"))
.LostGaze(() => Console.WriteLine("Bye..."));
Console.ReadKey(true);
(...)
}
public static class InteractorExtensions
{
public static GazeAwareBehavior WithGazeAware(this IMutableBehaviorsInteractor interactor);
(...)
}
public class GazeAwareBehavior : EventHandlingBase, IBehavior, IChecksummable
{
public const string HasGazeChangedToken = "HazGazeChanged";
public GazeAwareBehavior();
public GazeAwareBehavior(GazeAwareMode mode = GazeAwareMode.Normal, TimeSpan? delayTime = null);
public TimeSpan DelayTime { get; set; }
public GazeAwareMode Mode { get; set; }
public event EventHandler<HasGazeChangedEventArgs> HasGazeChanged;
}
Public Class VirtualInteractorAgent(Of TInteractor As IInteractor, TData)
Inherits ProviderInteractorAgent(Of IInteractorRepository(Of TInteractor))
Protected Sub New(agentId As String, defaultWindowId As String, repository As IInteractorRepository(Of TInteractor), createInteractorDelegate As CreateInteractorDelegate)
Protected ReadOnly Property DefaultWindowId As String
Protected ReadOnly Property Repository As IInteractorRepository(Of TInteractor)
Public Sub RemoveInteractor(interactorId As String)
Public Sub RemoveInteractors(ParamArray ids() As String)
Public Sub Suspend()
Public Sub [Resume]()
Public Sub Clear()
Protected Overrides Sub Dispose(disposing As Boolean)
Public Function AddInteractorFor(data As TData) As TInteractor
Public Function AddInteractorFor(data As TData, Optional parentId As String = "_RootId", Optional z As Double = 0, Optional windowId As String = Nothing, Optional id As String = Nothing) As TInteractor
Public Function FindInteractor(interactorId As String) As TInteractor
Public Function AddInteractorsFor(ParamArray datas() As TData) As IEnumerable(Of TInteractor)
Public Function FindInteractors(ParamArray ids() As String) As IEnumerable(Of TInteractor)
Public Delegate Function CreateInteractorDelegate(data As TData, Optional parentId As String = "_RootId", Optional z As Double = 0, Optional windowId As String = Nothing, Optional id As String = Nothing) As TInteractor
End Class
每当您在 C# 代码中看到 =>
时,您就是在查看 Lambda 表达式。 C# 中 (params) => body
的等价物是 VB 中的 Function(params) body
或 Sub(params) body
,具体取决于 body
是否求值。在你的例子中,你的两个 Lambdas 都包含 Console.WriteLine
,所以两者都不计算为一个值,所以两者都应该是 Sub
:
Dim interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent")
interactorAgent.AddInteractorFor(currentWindowBounds).
WithGazeAware().
HasGaze(Sub() Console.WriteLine("Hey there!")).
LostGaze(Sub() Console.WriteLine("Bye..."));
一般也可以通过参数的类型判断是使用Function
还是Sub
。在这种情况下,HasGaze
和 LostGaze
可能都有一个类型为 Action
或类似的参数,这意味着使用 Sub
。如果它是 Func
或类似的,那么您将使用 Function
。 Action
是一个没有 return 值的委托,Func
是一个有 return 值的委托。