2017-01-10 174 views
0

我正在學習和開發觸摸屏應用程序。 我想下面的示例代碼:WPF操作事件沒有被觸發

的XAML:

<Window x:Class = "WPFMultiTouchInput.MainWindow" 
    xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local = "clr-namespace:WPFMultiTouchInput" 
    mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> 

    <Window.Resources> 
     <MatrixTransform x:Key = "InitialMatrixTransform"> 
     <MatrixTransform.Matrix> 
      <Matrix OffsetX = "200" OffsetY = "200"/> 
     </MatrixTransform.Matrix> 
     </MatrixTransform> 
    </Window.Resources> 

    <Canvas> 
     <Rectangle Name = "manRect" Width = "321" Height = "241" 
     RenderTransform = "{StaticResource InitialMatrixTransform}" 
     IsManipulationEnabled = "true" Canvas.Left = "-70" Canvas.Top = "-170"> 
     <Rectangle.Fill> 
      <ImageBrush ImageSource = "Images/DSC_0076.JPG"/> 
     </Rectangle.Fill> 
     </Rectangle> 
    </Canvas> 

</Window> 

的代碼執行背後:

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

namespace WPFMultiTouchInput { 

    public partial class MainWindow : Window { 

     public MainWindow() { 
     InitializeComponent(); 
     } 

     void Window_ManipulationStarting(object sender, ManipulationStartingEventArgs e) { 
     e.ManipulationContainer = this; 
     e.Handled = true; 
     } 

     void Window_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) { 
     Rectangle rectToMove = e.OriginalSource as Rectangle; 
     Matrix rectsMatrix = ((MatrixTransform)rectToMove.RenderTransform).Matrix; 

     rectsMatrix.RotateAt(e.DeltaManipulation.Rotation, e.ManipulationOrigin.X, e.ManipulationOrigin.Y); 

     rectsMatrix.ScaleAt(e.DeltaManipulation.Scale.X, e.DeltaManipulation.Scale.X, 
      e.ManipulationOrigin.X, e.ManipulationOrigin.Y); 

     rectsMatrix.Translate(e.DeltaManipulation.Translation.X, 
      e.DeltaManipulation.Translation.Y); 

     rectToMove.RenderTransform = new MatrixTransform(rectsMatrix); 
     Rect containingRect = new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize); 

     Rect shapeBounds = rectToMove.RenderTransform.TransformBounds(new Rect(rectToMove.RenderSize)); 

     if (e.IsInertial && !containingRect.Contains(shapeBounds)) { 
      e.Complete(); 
     } 

     e.Handled = true; 
     } 

     void Window_InertiaStarting(object sender, ManipulationInertiaStartingEventArgs e) { 
     e.TranslationBehavior.DesiredDeceleration = 10.0 * 96.0/(1000.0 * 1000.0); 
     e.ExpansionBehavior.DesiredDeceleration = 0.1 * 96/(1000.0 * 1000.0); 
     e.RotationBehavior.DesiredDeceleration = 720/(1000.0 * 1000.0); 
     e.Handled = true; 
     } 

    } 
} 

但操縱事件不會被解僱。 樣品來自here。 我正在使用HP TouchSmart610顯示器作爲我的觸控設備。是否有任何特定的驅動程序需要安裝來處理Windows觸摸事件?

[編輯] 我正在遠程使用這個HP TouchSmart610觸摸設備。這是我的應用程序正在另一臺機器上運行。我懷疑,這可能是一個原因。不確定。任何想法?

+1

你不重視操控的事件處理程序到您的窗口(或任何他們想要連接的地方)。其實TutorialsPoint上的例子已經失效。 – Clemens

回答

0

你忘了附ManipulationStartingManipulationDeltaManipulationInertiaStarting事件處理程序到你的窗口:

<Window x:Class = "WPFMultiTouchInput.MainWindow" 
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:local = "clr-namespace:WPFMultiTouchInput" 
ManipulationStarting="Window_ManipulationStarting" 
ManipulationDelta="Window_ManipulationDelta" 
ManipulationInertiaStarting="Window_InertiaStarting" 
mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> 

原始樣品可以在這裏看到:Walkthrough: Creating Your First Touch Application