TouchDevice Classe

Definizione

Rappresenta un singolo input tocco prodotto da un dito su un 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
Ereditarietà
Implementazioni

Esempio

L'esempio seguente consente di creare modelli semplici su un Canvas oggetto trascinando due dita su un touchscreen. Ogni tocco è rappresentato da un TouchDevice oggetto in TouchEventArgs. Il motivo viene creato disegnando una linea tra i punti di tocco forniti dai tocchi. Questo esempio richiede una schermata compatibile con Il tocco di Windows.

Il markup seguente crea l'interfaccia utente, costituita da un Canvas oggetto centrato in una griglia e associa i gestori eventi per gli eventi di tocco.

<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>

Il codice seguente gestisce gli eventi di tocco. Quando viene premuto un tocco su Canvas, l'oggetto TouchDevice viene acquisito in Canvas. Quando il tocco viene sollevato, l'oggetto TouchDevice viene rilasciato. Quando un tocco si sposta su Canvas, viene controllato .Id Se lo spostamento proviene dal primo tocco, viene registrata la sua posizione. Se lo spostamento proviene dal secondo tocco, una linea viene disegnata dalla posizione del primo tocco alla posizione del secondo tocco.

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

Commenti

In genere si accede a usando TouchDevice la TouchEventArgs.TouchDevice proprietà . Un TouchDevice oggetto rappresenta un singolo tocco su uno schermo. Se sono presenti più tocchi, usare la Id proprietà per distinguerle tra di esse.

Nota

Questa classe contiene una richiesta di ereditarietà a livello di classe che si applica a tutti i membri. Viene SecurityException generata un'eccezione quando la classe derivata non dispone dell'autorizzazione di attendibilità totale. Per altre informazioni sulle richieste di sicurezza, vedere Richieste di collegamento e richieste di ereditarietà.

Costruttori

TouchDevice(Int32)

Viene chiamato dai costruttori nelle classi derivate per inizializzare la classe TouchDevice.

Proprietà

ActiveSource

Ottiene l'oggetto PresentationSource che segnala l'input per questo dispositivo.

Captured

Ottiene l'elemento che ha acquisito l'oggetto TouchDevice.

CaptureMode

Ottiene i criteri di acquisizione dell'oggetto TouchDevice.

DirectlyOver

Ottiene l'elemento che si trova direttamente sopra il punto di contatto di tocco.

Dispatcher

Ottiene l'oggetto Dispatcher associato a DispatcherObject.

(Ereditato da DispatcherObject)
Id

Ottiene l'identificatore univoco di TouchDevice, come fornito dal sistema operativo.

IsActive

Ottiene un valore che indica se il dispositivo è attivo.

Target

Ottiene l'elemento che riceve l'input dall'oggetto TouchDevice.

Metodi

Activate()

Aggiunge l'oggetto TouchDevice al sistema di messaggistica di input.

Capture(IInputElement)

Acquisisce un tocco nell'elemento specificato tramite la modalità di acquisizione Element.

Capture(IInputElement, CaptureMode)

Acquisisce un tocco nell'elemento specificato utilizzando l'oggetto CaptureMode specificato.

CheckAccess()

Determina se il thread chiamante ha accesso a DispatcherObject.

(Ereditato da DispatcherObject)
Deactivate()

Rimuove l'oggetto TouchDevice dal sistema di messaggistica di input.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetIntermediateTouchPoints(IInputElement)

Se sottoposto a override in una classe derivata, restituisce tutti i punti di tocco raccolti tra l'evento di tocco più recente e quello precedente.

GetTouchPoint(IInputElement)

Restituisce la posizione corrente del dispositivo a tocco relativa all'elemento specificato.

GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
OnCapture(IInputElement, CaptureMode)

Chiamato quanto un tocco è acquisito in un elemento.

OnManipulationEnded(Boolean)

Chiamato al termine di una manipolazione.

OnManipulationStarted()

Chiamato all'avvio di una manipolazione.

ReportDown()

Segnala che un tocco viene premuto in un elemento.

ReportMove()

Segnala che un tocco viene spostato in un elemento.

ReportUp()

Restituisce che un tocco è stato sollevato da un elemento.

SetActiveSource(PresentationSource)

Imposta l'oggetto PresentationSource che segnala l'input per questo dispositivo.

Synchronize()

Forza TouchDevice a sincronizzare l'interfaccia utente con i punti di tocco sottostanti.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)
VerifyAccess()

Impone che il thread chiamante abbia accesso a DispatcherObject.

(Ereditato da DispatcherObject)

Eventi

Activated

Si verifica quando TouchDevice viene aggiunto al sistema di messaggistica di input.

Deactivated

Si verifica quando l'oggetto TouchDevice viene rimosso dal sistema di messaggistica di input.

Updated

Si verifica quando viene inviato un messaggio di tocco.

Implementazioni dell'interfaccia esplicita

IManipulator.GetPosition(IInputElement)

Restituisce la posizione dell'oggetto IManipulator.

IManipulator.Id

Ottiene l'identificatore univoco di TouchDevice, come fornito dal sistema operativo.

IManipulator.ManipulationEnded(Boolean)

Si verifica al termine di una manipolazione.

Si applica a