-1

我正在处理以下要转换为 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
4

1 回答 1

3

每当您=>在 C# 代码中看到时,您就是在查看 Lambda 表达式。(params) => bodyC# 中的 isFunction(params) body或VB 中的等价物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的委托,并且是一个确实返回值的委托。

于 2018-11-05T00:47:17.480 回答