在Hololens 1的2D UWP应用程序中检测抽气事件

问题描述 投票:0回答:1

[我正在尝试在Hololens 1的2D UWP应用程序中检测到空击事件。我正在使用VS 2019进行开发。我遵循了BasicHologramHands and motion controllers in DirectX中的一些示例代码。这是我写的示例代码:

SpatialInputHandler.cs

public class SpatialInputHandler
{
    private SpatialInteractionManager interactionManager;
    private SpatialInteractionSourceState sourceState;
    public SpatialInputHandler()
    {
        interactionManager = SpatialInteractionManager.GetForCurrentView();
        interactionManager.SourcePressed += this.OnSourcePressed;
    }
    public SpatialInteractionSourceState CheckForInput()
    {
        SpatialInteractionSourceState sourceState = this.sourceState;
        this.sourceState = null;
        return sourceState;
    }
    public void OnSourcePressed(SpatialInteractionManager sender, SpatialInteractionSourceEventArgs args)
    {
        sourceState = args.State;
    }
}

MainPage.cs

public sealed partial class MainPage : Page
{
    private SpatialInputHandler spatialInputHandler;
    DispatcherTimer dispatcherTimer;
    public MainPage()
    {
        this.InitializeComponent();
        spatialInputHandler = new SpatialInputHandler();
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = TimeSpan.FromMilliseconds(60);
        dispatcherTimer.Tick += DispatcherTimer_Tick;
        dispatcherTimer.Start();
    }
    private void DispatcherTimer_Tick(object sender, object e)
    {
        SpatialInteractionSourceState pointerState = spatialInputHandler.CheckForInput();
        if (pointerState != null && pointerState.IsPressed)
        {
            textBlock2.Text = "airtap detected";
        }
        if(pointerState == null)
        {
            textBlock2.Text = "no airtap detected";
        }
    }
}

在我的代码中,我总是将pointerState值设置为null。有人可以帮我解决这个问题。谢谢!

c# uwp hololens
1个回答
0
投票

因为您正在HoloLens上开发2d平面应用程序,所以SpatialInteractionManager类不是针对这种情况而设计的,而是针对全息应用程序的。

对于平面应用程序,它将在外壳中运行,因此,我们需要处理路由事件,这些事件适用于输入和用户交互场景(例如,触摸,鼠标等)。例如,您的UI可能有一个按钮(SpatialInteractionManagerButton基类继承)并处理Click或Tap事件:

XAML:

Button

后端:

UIElement

这些事件可以通过您在HoloLens上的轻敲互动来正确响应。

有关更多详细信息,请参阅我们的本文档:UIElement

如果您可以共享有关您希望空气龙头如何与您的应用交互的更多详细信息,我们很乐意帮助您前进。

© www.soinside.com 2019 - 2024. All rights reserved.