【问题标题】:Mapping TouchMove Events to MouseMove Events in WPF在 WPF 中将 TouchMove 事件映射到 MouseMove 事件
【发布时间】:2012-12-10 14:50:03
【问题描述】:

我正在 WPF 应用程序中实现一个简单的拖放功能。我希望这个应用程序既可以在不支持触控的桌面上运行,也可以在仅支持触控的平板电脑上运行。

目前我有一个 MouseMove 和 TouchMove 处理程序,它们都实现了相同的逻辑(启动 DoDragDrop())。

如何将输入从触摸路由到鼠标处理程序或反之亦然以减少冗余代码?进一步如何将一个简单的点击路由到点击事件?

【问题讨论】:

    标签: wpf mouseevent touch-event


    【解决方案1】:

    我刚刚做了一个快速测试,一种方法是创建一个全局事件处理程序。

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多