Bagikan melalui


TouchDevice Kelas

Definisi

Mewakili input sentuhan tunggal yang dihasilkan oleh jari pada layar sentuh.

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

Contoh

Contoh berikut memungkinkan Anda membuat pola sederhana pada dengan Canvas menyeret dua jari pada layar sentuh. Setiap sentuhan diwakili oleh TouchDevice di TouchEventArgs. Pola dibuat dengan menggambar garis di antara titik sentuh yang disediakan oleh sentuhan. Contoh ini memerlukan layar yang kompatibel dengan Windows Touch.

Markup berikut membuat antarmuka pengguna, yang terdiri dari yang berpusat Canvas di kisi, dan melampirkan penanganan aktivitas untuk peristiwa sentuh.

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

Kode berikut menangani peristiwa sentuhan. Ketika sentuhan ditekan pada Canvas, TouchDevice diambil ke Canvas. Ketika sentuhan diangkat, dilepaskan TouchDevice . Ketika sentuhan bergerak pada Canvas, Id dicentang. Jika pemindahan berasal dari sentuhan pertama, lokasinya akan direkam. Jika gerakan berasal dari sentuhan kedua, garis digambar dari posisi sentuhan pertama ke posisi sentuhan kedua.

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

Keterangan

Anda biasanya mengakses dengan TouchDevice menggunakan TouchEventArgs.TouchDevice properti . mewakili TouchDevice satu sentuhan pada layar. Jika ada beberapa sentuhan, gunakan Id properti untuk membedakannya.

Catatan

Kelas ini berisi permintaan warisan di tingkat kelas yang berlaku untuk semua anggota. SecurityException dilemparkan ketika kelas turunan tidak memiliki izin kepercayaan penuh. Untuk informasi selengkapnya tentang tuntutan keamanan, lihat Permintaan Tautan dan Permintaan Warisan.

Konstruktor

TouchDevice(Int32)

Dipanggil dari konstruktor di kelas turunan TouchDevice untuk menginisialisasi kelas.

Properti

ActiveSource

PresentationSource Mendapatkan input pelaporan untuk perangkat ini.

Captured

Mendapatkan elemen yang menangkap TouchDevice.

CaptureMode

Mendapatkan kebijakan penangkapan dari TouchDevice.

DirectlyOver

Mendapatkan elemen bahwa titik kontak sentuh langsung berakhir.

Dispatcher

Mendapatkan ini DispatcherDispatcherObject dikaitkan dengan.

(Diperoleh dari DispatcherObject)
Id

Mendapatkan pengidentifikasi unik dari TouchDevice, seperti yang disediakan oleh sistem operasi.

IsActive

Mendapatkan nilai yang menunjukkan apakah perangkat aktif.

Target

Mendapatkan elemen yang menerima input dari TouchDevice.

Metode

Activate()

Menambahkan ke TouchDevice sistem olahpesan input.

Capture(IInputElement)

Mengambil sentuhan ke elemen yang ditentukan dengan menggunakan Element mode pengambilan.

Capture(IInputElement, CaptureMode)

Mengambil sentuhan ke elemen yang ditentukan dengan menggunakan yang ditentukan CaptureMode.

CheckAccess()

Menentukan apakah utas panggilan memiliki akses ke ini DispatcherObject.

(Diperoleh dari DispatcherObject)
Deactivate()

TouchDevice Menghapus dari sistem olahpesan input.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetIntermediateTouchPoints(IInputElement)

Saat ditimpa di kelas turunan, mengembalikan semua titik sentuh yang dikumpulkan antara peristiwa sentuhan terbaru dan sebelumnya.

GetTouchPoint(IInputElement)

Mengembalikan posisi perangkat sentuh saat ini relatif terhadap elemen yang ditentukan.

GetType()

Mendapatkan instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari yang saat ini Object.

(Diperoleh dari Object)
OnCapture(IInputElement, CaptureMode)

Dipanggil saat sentuhan diambil ke elemen.

OnManipulationEnded(Boolean)

Dipanggil ketika manipulasi telah berakhir.

OnManipulationStarted()

Dipanggil ketika manipulasi dimulai.

ReportDown()

Melaporkan bahwa sentuhan ditekan pada elemen.

ReportMove()

Melaporkan bahwa sentuhan bergerak melintasi elemen.

ReportUp()

Melaporkan bahwa sentuhan diangkat dari elemen.

SetActiveSource(PresentationSource)

Menyetel PresentationSource input pelaporan untuk perangkat ini.

Synchronize()

Memaksa untuk menyinkronkan antarmuka pengguna dengan titik sentuh yang mendasar TouchDevice .

ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)
VerifyAccess()

Memberlakukan bahwa utas panggilan memiliki akses ke ini DispatcherObject.

(Diperoleh dari DispatcherObject)

Acara

Activated

Terjadi ketika TouchDevice ditambahkan ke sistem olahpesan input.

Deactivated

Terjadi ketika TouchDevice dihapus dari sistem olahpesan input.

Updated

Terjadi saat pesan sentuhan dikirim.

Implementasi Antarmuka Eksplisit

IManipulator.GetPosition(IInputElement)

Mengembalikan posisi IManipulator objek.

IManipulator.Id

Mendapatkan pengidentifikasi unik dari TouchDevice yang disediakan oleh sistem operasi.

IManipulator.ManipulationEnded(Boolean)

Terjadi ketika manipulasi telah berakhir.

Berlaku untuk