Touch 类

定义

提供一个应用程序级服务,该服务处理来自操作系统的多触摸输入并引发 FrameReported 事件。

public ref class Touch abstract sealed
public static class Touch
type Touch = class
Public Class Touch
继承
Touch

示例

以下示例使你能够通过在触摸屏上拖动两根手指来创建 Canvas 简单的模式。 每个触摸都由一个 TouchDevice。 通过绘制触摸提供的触摸点之间的线条来创建图案。 此示例需要Windows兼容触摸的屏幕。

以下标记创建用户界面,该用户界面由居中居中设置的Canvas用户界面组成Grid

<Window x:Class="WpfTouchFrameSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="525" Width="525">
    <Grid>
        <Canvas x:Name="canvas1"
                Width="500" Height="500"
                Background="Black" />     
    </Grid>
</Window>

以下代码处理 FrameReported 事件。 当触摸按下时 Canvas,会 TouchDevice 捕获到该控件 Canvas。 当触摸解除时,将 TouchDevice 释放触摸。 当触摸移动过 Canvas时, Id 将选中它。 如果移动来自第一次触摸,则会记录其位置。 如果移动来自第二次触摸,则从第一次触摸的位置到第二次触摸的位置绘制一条线。

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfTouchFrameSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Variables for tracking the position of two points.
        Point pt1, pt2 = new Point();
        
        public MainWindow()
        {
            InitializeComponent();
            Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);
        }

        void Touch_FrameReported(object sender, TouchFrameEventArgs e)
        {
            if (this.canvas1 != null)
            {
                foreach (TouchPoint _touchPoint in e.GetTouchPoints(this.canvas1))
                {
                    if (_touchPoint.Action == TouchAction.Down)
                    {
                        // Clear the canvas and capture the touch to it.
                        this.canvas1.Children.Clear();
                        _touchPoint.TouchDevice.Capture(this.canvas1);
                    }

                    else if (_touchPoint.Action == TouchAction.Move && e.GetPrimaryTouchPoint(this.canvas1) != null)
                    {   
                        // This is the first (primary) touch point. Just record its position.
                        if (_touchPoint.TouchDevice.Id == e.GetPrimaryTouchPoint(this.canvas1).TouchDevice.Id)
                        {
                            pt1.X = _touchPoint.Position.X;
                            pt1.Y = _touchPoint.Position.Y;
                        }

                        // This is not the first touch point. Draw a line from the first point to this one.
                        else if (_touchPoint.TouchDevice.Id != e.GetPrimaryTouchPoint(this.canvas1).TouchDevice.Id)
                        {
                            pt2.X = _touchPoint.Position.X;
                            pt2.Y = _touchPoint.Position.Y;

                            Line _line = new Line();
                            _line.Stroke = new RadialGradientBrush(Colors.White, Colors.Black);
                            _line.X1 = pt1.X;
                            _line.X2 = pt2.X;
                            _line.Y1 = pt1.Y;
                            _line.Y2 = pt2.Y;
                            _line.StrokeThickness = 2;
                            this.canvas1.Children.Add(_line);
                        }
                    }

                    else if (_touchPoint.Action == TouchAction.Up)
                    {
                        // If this touch is captured to the canvas, release it.
                        if (_touchPoint.TouchDevice.Captured == this.canvas1)
                        {
                            this.canvas1.ReleaseTouchCapture(_touchPoint.TouchDevice);
                        }
                    }
                }                        
            }
        }
    }
}
Class MainWindow
    ' Variables for tracking the position of two points.
    Private pt1, pt2 As Point

    Public Sub New()
        InitializeComponent()
        AddHandler Touch.FrameReported, AddressOf Touch_FrameReported
    End Sub

    Private Sub Touch_FrameReported(ByVal sender As System.Object, ByVal e As System.Windows.Input.TouchFrameEventArgs)
        If (canvas1 IsNot Nothing) Then
            For Each _touchPoint In e.GetTouchPoints(Me.canvas1)

                If _touchPoint.Action = TouchAction.Down Then
                    ' Clear the canvas and capture the touch to it.
                    canvas1.Children.Clear()
                    _touchPoint.TouchDevice.Capture(canvas1)

                ElseIf _touchPoint.Action = TouchAction.Move Then
                    ' This is the first (primary) touch point. Just record its position.
                    If _touchPoint.TouchDevice.Id = e.GetPrimaryTouchPoint(Me.canvas1).TouchDevice.Id Then
                        pt1.X = _touchPoint.Position.X
                        pt1.Y = _touchPoint.Position.Y

                        ' This is not the first touch point; draw a line from the first point to this one.
                    ElseIf _touchPoint.TouchDevice.Id <> e.GetPrimaryTouchPoint(Me.canvas1).TouchDevice.Id Then
                        pt2.X = _touchPoint.Position.X
                        pt2.Y = _touchPoint.Position.Y

                        Dim _line As New Line()
                        _line.Stroke = New RadialGradientBrush(Colors.White, Colors.Black)
                        _line.X1 = pt1.X
                        _line.X2 = pt2.X
                        _line.Y1 = pt1.Y
                        _line.Y2 = pt2.Y

                        _line.StrokeThickness = 2
                        Me.canvas1.Children.Add(_line)
                    End If

                ElseIf _touchPoint.Action = TouchAction.Up Then
                    ' If this touch is captured to the canvas, release it.
                    If (_touchPoint.TouchDevice.Captured Is canvas1) Then
                        canvas1.ReleaseTouchCapture(_touchPoint.TouchDevice)
                    End If
                End If
            Next
        End If
    End Sub
End Class

注解

FrameReported 事件包含在 Windows Presentation Foundation (WPF) 中,以支持与 Silverlight 的兼容性。 如果不必确保与 Silverlight 的兼容性,请使用触摸事件,例如TouchDown,on 和、on TouchMoveUIElementUIElement3DContentElement

FrameReported 事件不使用与其他WPF输入事件相同的事件模型,例如 TouchDownTouchMove。 事件 FrameReported 不是公开为可能通过 UI 的对象树路由的特定于元素的事件,而是在应用程序级别处理的单个事件。 因此,不能使用 sender 事件处理程序的参数来确定触摸的元素。

使用此方法 TouchFrameEventArgs 获取 TouchPoint 与触摸事件相关的值。 从中 TouchPoint,你可以获取 Position 触摸并确定 TouchAction 是一个 DownMove还是 Up 操作。 还可以使用该 TouchPoint 来获取 TouchDevice。 从中 TouchDevice,你可以确定设备 Id 并获取有关所触摸元素的信息。

活动

名称 说明
FrameReported

发送触摸消息时发生。

适用于