TouchDevice TouchDevice TouchDevice TouchDevice Class

定义

表示触摸屏上的一个手指产生的单个触摸输入。Represents a single touch input produced by a finger on a touchscreen.

public ref class TouchDevice abstract : System::Windows::Input::InputDevice, System::Windows::Input::IManipulator
public abstract class TouchDevice : System.Windows.Input.InputDevice, System.Windows.Input.IManipulator
type TouchDevice = class
    inherit InputDevice
    interface IManipulator
Public MustInherit Class TouchDevice
Inherits InputDevice
Implements IManipulator
继承
实现

示例

下面的示例Canvas通过将两个手指拖到触摸屏上,使你可以在上创建简单的模式。The following example enables you to create simple patterns on a Canvas by dragging two fingers on a touchscreen. 每个触控由TouchDevice TouchEventArgs中的表示。Each touch is represented by a TouchDevice in the TouchEventArgs. 通过在触控提供的触摸点之间绘制一条线来创建模式。The pattern is created by drawing a line between the touch points that are provided by the touches. 此示例需要 Windows 触摸屏兼容屏幕。This example requires a Windows Touch-compatible screen.

下面的标记将创建一个Canvas用户界面,该用户界面由网格中的中心组成,并附加触控事件的事件处理程序。The following markup creates the user interface, which consists of a Canvas that is centered in a grid, and attaches the event handlers for the touch events.

<Window x:Class="WpfTouchEventsSample.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"
                TouchDown="canvas_TouchDown"
                TouchMove="canvas_TouchMove"
                TouchUp="canvas_TouchUp" />
    </Grid>
</Window>

下面的代码处理触控事件。The following code handles the touch events. 在上Canvas按下触摸时TouchDevice ,会将捕获到CanvasWhen a touch is pressed on the Canvas, the TouchDevice is captured to the Canvas. 在提起触摸时, TouchDevice将释放。When the touch is lifted, the TouchDevice is released. 当触摸在上Canvas移动时Id ,将选中。When a touch moves on the Canvas, the Id is checked. 如果移动来自第一次触摸,则会记录其位置。If the move came from the first touch, its location is recorded. 如果移动从第二个触摸开始,则从第一个触摸的位置到第二个触摸的位置绘制线条。If the move came from the second touch, a line is drawn from the position of the first touch to the position of the second touch.

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

namespace WpfTouchEventsSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Variables to track the first two touch points 
        // and the ID of the first touch point.
        private Point pt1, pt2 = new Point();
        private int firstTouchId = -1;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void canvas_TouchDown(object sender, TouchEventArgs e)
        {
            Canvas _canvas = (Canvas)sender as Canvas;
            if (_canvas != null)
            {
                _canvas.Children.Clear();
                e.TouchDevice.Capture(_canvas);

                // Record the ID of the first touch point if it hasn't been recorded.
                if (firstTouchId == -1)
                    firstTouchId = e.TouchDevice.Id;
            }
        }

        private void canvas_TouchMove(object sender, TouchEventArgs e)
        {
            Canvas _canvas = (Canvas)sender as Canvas;
            if (_canvas != null)
            {
                TouchPoint tp = e.GetTouchPoint(_canvas);
                // This is the first touch point; just record its position.
                if (e.TouchDevice.Id == firstTouchId)
                {
                    pt1.X = tp.Position.X;
                    pt1.Y = tp.Position.Y;
                }
                // This is not the first touch point; draw a line from the first point to this one.
                else if (e.TouchDevice.Id != firstTouchId)
                {
                    pt2.X = tp.Position.X;
                    pt2.Y = tp.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;
                    _canvas.Children.Add(_line);
                }
            }
        }

        private void canvas_TouchUp(object sender, TouchEventArgs e)
        {
            Canvas _canvas = (Canvas)sender as Canvas;
            if (_canvas != null && e.TouchDevice.Captured == _canvas)
            {
                _canvas.ReleaseTouchCapture(e.TouchDevice);
            }
        }
    }
}
Class MainWindow
    ' Variables to track the first two touch points 
    ' and the ID of the first touch point.
    Private pt1, pt2 As Point
    Private firstTouchId As Integer = -1
    ' Touch Down
    Private Sub canvas_TouchDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.TouchEventArgs)
        Dim _canvas As Canvas = CType(sender, Canvas)
        If (_canvas IsNot Nothing) Then
            _canvas.Children.Clear()
            e.TouchDevice.Capture(_canvas)

            ' Record the ID of the first touch point if it hasn't been recorded.
            If firstTouchId = -1 Then
                firstTouchId = e.TouchDevice.Id
            End If
        End If
    End Sub
    ' Touch Move
    Private Sub canvas_TouchMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.TouchEventArgs)
        Dim _canvas As Canvas = CType(sender, Canvas)
        If (_canvas IsNot Nothing) Then
            Dim tp = e.GetTouchPoint(_canvas)
            ' This is the first touch point; just record its position.
            If e.TouchDevice.Id = firstTouchId Then
                pt1.X = tp.Position.X
                pt1.Y = tp.Position.Y

                ' This is not the first touch point; draw a line from the first point to this one.
            ElseIf e.TouchDevice.Id <> firstTouchId Then
                pt2.X = tp.Position.X
                pt2.Y = tp.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
                _canvas.Children.Add(_line)
            End If
        End If
    End Sub
    ' Touch Up
    Private Sub canvas_TouchUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.TouchEventArgs)
        Dim _canvas As Canvas = CType(sender, Canvas)
        If (_canvas IsNot Nothing AndAlso e.TouchDevice.Captured Is _canvas) Then
            _canvas.ReleaseTouchCapture(e.TouchDevice)

        End If
    End Sub
End Class

注解

TouchDevice 通常TouchEventArgs.TouchDevice使用属性访问。You typically access a TouchDevice by using the TouchEventArgs.TouchDevice property. TouchDevice表示屏幕上的单个触摸。A TouchDevice represents a single touch on a screen. 如果存在多个接触,请使用Id属性来区分它们。If multiple touches are present, use the Id property to distinguish between them.

备注

此类包含类级别的继承要求,该要求适用于所有成员。This class contains an inheritance demand at the class level that applies to all members. 当派生类不具有完全信任权限时,将引发。SecurityExceptionA SecurityException is thrown when the derived class does not have full-trust permission. 有关安全要求的详细信息,请参阅链接要求继承要求For more information about security demands, see Link Demands and Inheritance Demands.

构造函数

TouchDevice(Int32) TouchDevice(Int32) TouchDevice(Int32) TouchDevice(Int32)

从派生类中的构造函数中调用,用于初始化 TouchDevice 类。Called from constructors in derived classes to initialize the TouchDevice class.

属性

ActiveSource ActiveSource ActiveSource ActiveSource

获取报告此设备的输入的 PresentationSourceGets the PresentationSource that is reporting input for this device.

Captured Captured Captured Captured

获取捕获了 TouchDevice 的元素。Gets the element that captured the TouchDevice.

CaptureMode CaptureMode CaptureMode CaptureMode

获取 TouchDevice 的捕获策略。Gets the capture policy of the TouchDevice.

DirectlyOver DirectlyOver DirectlyOver DirectlyOver

获取位于触摸触点正下方的元素。Gets the element that the touch contact point is directly over.

Dispatcher Dispatcher Dispatcher Dispatcher

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)
Id Id Id Id

获取由操作系统提供的 TouchDevice 的唯一标识符。Gets the unique identifier of the TouchDevice, as provided by the operating system.

IsActive IsActive IsActive IsActive

获取一个值,该值指示设备是否为活动设备。Gets a value that indicates whether the device is active.

Target Target Target Target

获取从 TouchDevice 中接收输入的元素。Gets the element that receives input from the TouchDevice.

方法

Activate() Activate() Activate() Activate()

TouchDevice 添加到输入消息系统中。Adds the TouchDevice to the input messaging system.

Capture(IInputElement) Capture(IInputElement) Capture(IInputElement) Capture(IInputElement)

使用 Element 捕获模式将触摸屏输入捕获到指定元素。Captures a touch to the specified element by using the Element capture mode.

Capture(IInputElement, CaptureMode) Capture(IInputElement, CaptureMode) Capture(IInputElement, CaptureMode) Capture(IInputElement, CaptureMode)

使用指定的 CaptureMode 将触摸屏输入捕获到指定元素。Captures a touch to the specified element by using the specified CaptureMode.

CheckAccess() CheckAccess() CheckAccess() CheckAccess()

确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
Deactivate() Deactivate() Deactivate() Deactivate()

从输入消息系统中移除 TouchDeviceRemoves the TouchDevice from the input messaging system.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetIntermediateTouchPoints(IInputElement) GetIntermediateTouchPoints(IInputElement) GetIntermediateTouchPoints(IInputElement) GetIntermediateTouchPoints(IInputElement)

在派生类中重写时,返回在最近触摸事件与上一个触摸事件之间收集到的所有触点。When overridden in a derived class, returns all touch points that are collected between the most recent and previous touch events.

GetTouchPoint(IInputElement) GetTouchPoint(IInputElement) GetTouchPoint(IInputElement) GetTouchPoint(IInputElement)

返回触摸设备相对于指定元素的当前位置。Returns the current position of the touch device relative to the specified element.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OnCapture(IInputElement, CaptureMode) OnCapture(IInputElement, CaptureMode) OnCapture(IInputElement, CaptureMode) OnCapture(IInputElement, CaptureMode)

在元素上捕获触摸屏输入时调用。Called when a touch is captured to an element.

OnManipulationEnded(Boolean) OnManipulationEnded(Boolean) OnManipulationEnded(Boolean) OnManipulationEnded(Boolean)

在操作结束后调用。Called when a manipulation has ended.

OnManipulationStarted() OnManipulationStarted() OnManipulationStarted() OnManipulationStarted()

在操作开始后调用。Called when a manipulation is started.

ReportDown() ReportDown() ReportDown() ReportDown()

报告对元素按下了触摸屏输入。Reports that a touch is pressed on an element.

ReportMove() ReportMove() ReportMove() ReportMove()

报告触摸屏输入跨元素移动。Reports that a touch is moving across an element.

ReportUp() ReportUp() ReportUp() ReportUp()

报告触摸屏输入已从元素移开。Reports that a touch was lifted from an element.

SetActiveSource(PresentationSource) SetActiveSource(PresentationSource) SetActiveSource(PresentationSource) SetActiveSource(PresentationSource)

设置报告此设备的输入的 PresentationSourceSets the PresentationSource that is reporting input for this device.

Synchronize() Synchronize() Synchronize() Synchronize()

强制 TouchDevice 将用户界面与基础触点同步。Forces the TouchDevice to synchronize the user interface with underlying touch points.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
VerifyAccess() VerifyAccess() VerifyAccess() VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

事件

Activated Activated Activated Activated

TouchDevice 添加到输入消息传送系统时发生。Occurs when the TouchDevice is added to the input messaging system.

Deactivated Deactivated Deactivated Deactivated

从输入消息系统移除 TouchDevice 时发生。Occurs when the TouchDevice is removed from the input messaging system.

Updated Updated Updated Updated

在发送触摸消息时发生。Occurs when a touch message is sent.

显式界面实现

IManipulator.GetPosition(IInputElement) IManipulator.GetPosition(IInputElement) IManipulator.GetPosition(IInputElement) IManipulator.GetPosition(IInputElement)

返回 IManipulator 对象的位置。Returns the position of the IManipulator object.

IManipulator.Id IManipulator.Id IManipulator.Id IManipulator.Id

获取由操作系统提供的 TouchDevice 的唯一标识符。Gets the unique identifier of the TouchDevice as provided by the operating system.

IManipulator.ManipulationEnded(Boolean) IManipulator.ManipulationEnded(Boolean) IManipulator.ManipulationEnded(Boolean) IManipulator.ManipulationEnded(Boolean)

在操作结束后发生。Occurs when a manipulation has ended.

安全性

UIPermission
用于从此类派生。for deriving from this class. 安全操作: InheritanceDemandSecurity action: InheritanceDemand. 关联的枚举:UnrestrictedAssociated enumeration: Unrestricted

适用于