I'm dealing with the following C# code that I want to convert to VB.NET:
var interactorAgent = host.InitializeVirtualInteractorAgent(currentWindowHandle, "ConsoleWindowAgent");
interactorAgent
.AddInteractorFor(currentWindowBounds)
.WithGazeAware()
.HasGaze(() => Console.WriteLine("Hey there!"))
.LostGaze(() => Console.WriteLine("Bye..."));
I want to get a callback on a function if ".HasGaze" or ".LostGaze" occured.
I think I have to use "AddHandler" instead of "=>", but I'm lost on how to do that in VB.NET. Am I correct that I should use AddHandler here?
I think the difficult code is located in this declaration:
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;
}
But I'm not sure about it.
This is more of the code.
If anybody can shed some light on what I need to do, I'd be really glad.
Thank you.
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
Whenever you see
=>
in C# code then you're looking at a Lambda expression. The equivalent of(params) => body
in C# isFunction(params) body
orSub(params) body
in VB, depending on whetherbody
evaluates to a value or not. In your case, both your Lambdas containConsole.WriteLine
, so neither evaluates to a value, so both should beSub
:You can also generally tell whether to use
Function
orSub
by the type of the parameter. In this case, bothHasGaze
andLostGaze
probably have a parameter of typeAction
or similar, which means usingSub
. If it wasFunc
or similar then you'd useFunction
.Action
is a delegate that doesn't return a value andFunc
is a delegate that does return a value.