我正在处理以下要转换为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
答案 0 :(得分:3)
每当在C#代码中看到=>
时,您就在看Lambda表达式。 C#中(params) => body
的等效项是VB中的Function(params) body
或Sub(params) body
,这取决于body
是否求值。在您的情况下,两个Lambda都包含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
是一个不返回值的委托,而Func
是一个确实返回值的委托。