WPF操作事件未被触发

时间:2017-01-10 09:48:39

标签: c# wpf touch

我正在研究和开发触摸屏应用程序。 我尝试了以下示例代码:

的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 个答案:

答案 0 :(得分: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