2012-12-10 40 views
2

我在WPF應用程序中實現了一個簡單的拖放功能。我希望此應用程序可以在沒有觸摸支持的桌面上運行,也可以在僅支持觸摸的平板電腦上運行。將TouchMove事件映射到WPM中的MouseMove事件

目前我有一個MouseMove和TouchMove處理程序,都執行相同的邏輯(啓動DoDragDrop())。

如何將觸摸輸入路由到鼠標處理程序或反之亦然以減少冗餘代碼?進一步如何將一個簡單的點擊路由到點擊事件?

回答

3

我剛做了一個快速測試,一種方法是創建一個全局事件處理程序。

由於TouchEventArgsMouseButtonEventArgs派生從InputEventArgs,全局處理程序將只實現InputEventArgs

/// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     //private void Button_TouchMove(object sender, TouchEventArgs e) 
     //{ 
      // TouchEventArgs derives from InputEventArgs 
     //} 

     // private void Button_MouseMove(object sender, MouseButtonEventArgs e) 
     //{ 
      // MouseButtonEventArgs derives from InputEventArgs 
     //} 

     private void GlobalHandler(object sender, InputEventArgs e) 
     { 
      // This will fire for both TouchEventArgs and MouseButtonEventArgs 

      // If you need specific information from the event args you can just cast. 
      // e.g. var args = e as MouseButtonEventArgs; 
     } 

    } 

的XAML:

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid > 
     <Button MouseMove="GlobalHandler" TouchMove="GlobalHandler"/> 
    </Grid> 
</Window> 

希望這有助於