简体   繁体   English

在 Hololens 1 的 2D UWP 应用程序中检测 air-tap 事件

[英]detecting air-tap event in an 2D UWP application for Hololens 1

I am trying to detect an air-tap event in my 2D UWP application for Hololens 1. I am using VS 2019 for my development.我正在尝试在 Hololens 1 的 2D UWP 应用程序中检测空气敲击事件。我正在使用 VS 2019 进行开发。 I have followed some sample code from BasicHologram and Hands and motion controllers in DirectX .我遵循了一些来自BasicHologramHands 以及 DirectX 中的运动控制器的示例代码。 Here is my sample code I wrote:这是我编写的示例代码:

SpatialInputHandler.cs空间输入处理器.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主页.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";
        }
    }
}

In my code I am always getting pointerState value as null.在我的代码中,我总是将pointerState值设为 null。 Could someone please help me how to figure this out.有人可以帮助我如何解决这个问题。 Thanks!谢谢!

Because you are develeping a 2d flat app on HoloLens, the SpatialInteractionManager class is not designed for such scenario, it's for holographic apps.因为您正在 HoloLens 上开发 2d 平面应用程序,所以SpatialInteractionManager类不是为这种情况设计的,而是为全息应用程序设计的。

For a flat app, it will run in the shell and as such we would need to handle routed events which are for input and user interaction scenarios(eg touch, mouse, etc…).对于平面应用程序,它将在 shell 中运行,因此我们需要处理用于输入和用户交互场景(例如触摸、鼠标等)的路由事件。 For example, your UI might have a button( Button inherited from the UIElement base class) and handle the Click or Tap event:例如,您的 UI 可能有一个按钮(从UIElement基类继承的Button )并处理 Click 或 Tap 事件:

XAML: XAML:

<Grid>
        <Button Name="btnClick" Content="Click" Width="100" Height="50" Click="BtnClick_Click" Tapped="BtnClick_Tapped" />
    </Grid>

Backend:后端:

private void BtnClick_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("Clicked");
}

private void BtnClick_Tapped(object sender, TappedRoutedEventArgs e)
{
    Debug.WriteLine("Tapped");
}

These events can respond correctly with your air-tap interaction on HoloLens.这些事件可以通过 HoloLens 上的隔空敲击交互正确响应。

For more details, check our this documentation: Events and routed events overview有关更多详细信息,请查看我们的此文档: 事件和路由事件概述

If you can share more details about how you want the air tap to interact with your app, we're happy to help you to move forward.如果您可以分享更多有关您希望 air tap 如何与您的应用进行交互的详细信息,我们很乐意帮助您继续前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM