简体   繁体   English

如何在uwp中获得触摸输入?

[英]How can I get touch input in uwp?

I need that if user touch on canvas control with one finger, drawing process must start and if user touch on the canvas control with two finger, canvas area must scroll/pan. 我需要如果用户用一根手指触摸画布控件,则绘图过程必须开始,并且如果用户用两根手指触摸画布控件,则画布区域必须滚动/平移。 How can I do this? 我怎样才能做到这一点?

Thanks 谢谢

We can use Pointer events to detect multiple fingers input in UWP. 我们可以使用Pointer事件来检测UWP中的多个手指输入。 For example: 例如:

<ScrollViewer x:Name="scrollViewer" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden"
              VerticalScrollMode="Disabled" HorizontalScrollMode="Disabled">
    <canvas:CanvasControl x:Name="canvasControl" Draw="CanvasControl_Draw" CreateResources="canvasControl_CreateResources" ClearColor="CornflowerBlue"
                          PointerPressed="canvasControl_PointerPressed" PointerReleased="canvasControl_PointerReleased" PointerExited="canvasControl_PointerReleased"
                          PointerCanceled="canvasControl_PointerPressed"
                          PointerMoved="canvasControl_PointerMoved" Width="1200">
    </canvas:CanvasControl>
</ScrollViewer>

As you can see, I put a ScrollViewer out of the canvas:CanvasControl , so will the canvas be able to be scrolled. 如您所见,我将ScrollViewer放置在canvas:CanvasControl ,因此可以滚动画布。 Code behind: 后面的代码:

private CanvasRenderTarget renderTarget;

private async void CanvasControl_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
{
    args.DrawingSession.DrawImage(renderTarget);
}

private void canvasControl_CreateResources(CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
{
    renderTarget = new CanvasRenderTarget(sender, (float)sender.ActualWidth, (float)sender.ActualHeight);
}

private List<PointerPoint> m_pt = new List<PointerPoint>();

private void canvasControl_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    PointerPoint cp = e.GetCurrentPoint(canvasControl);
    m_pt.Add(cp);
    if (m_pt.Count == 2)
    {
        scrollViewer.HorizontalScrollMode = ScrollMode.Enabled;
        scrollViewer.VerticalScrollMode = ScrollMode.Enabled;
    }
}

private void canvasControl_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    if (m_pt.Count == 1)
        canvasControl.Invalidate();
    scrollViewer.HorizontalScrollMode = ScrollMode.Disabled;
    scrollViewer.VerticalScrollMode = ScrollMode.Disabled;
    m_pt.Clear();
}

private void canvasControl_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    if (m_pt.Count == 1)
    {
        var pt = e.GetCurrentPoint(canvasControl);
        using (var ds = renderTarget.CreateDrawingSession())
        {
            ds.DrawCircle(new Vector2((float)pt.Position.X, (float)pt.Position.Y), 2, Colors.Black);
        }
    }
    else
    {
        if (m_pt.Count > 2)
            m_pt.Clear();
    }
}

When draw with one finger, in my demo the canvas will update its layout only when the pointer is released. 用一根手指绘制时,在我的演示中,画布仅在释放指针时才会更新其布局。 You can change the code to let the canvas to update every time when the pointer is moved. 您可以更改代码以使画布在每次移动指针时进行更新。 But I think for this, you will need to replace the canvas:CanvasControl with another Win2D control, for more information, you can refer to CanvasControl.Invalidate Method . 但是我认为,您需要用另一个Win2D控件替换canvas:CanvasControl ,有关更多信息,您可以参考CanvasControl.Invalidate Method

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

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