3

WPFアプリケーションに単純なドラッグアンドドロップ機能を実装しています。このアプリケーションを、タッチサポートのないデスクトップとタッチサポートのみのタブレットの両方で実行したいと思います。

現在、MouseMoveハンドラーとTouchMoveハンドラーがあり、どちらも同じロジックを実装しています(DoDragDrop()の開始)。

冗長なコードを減らすために、タッチからマウスハンドラーに、またはその逆に入力をルーティングするにはどうすればよいですか?さらに、単純なタップをクリックイベントにルーティングするにはどうすればよいでしょうか。

4

1 に答える 1

4

簡単なテストを行ったところです。これを行う 1 つの方法は、グローバル イベント ハンドラーを作成することです。

TouchEventArgsおよびMouseButtonEventArgsから派生するため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>

お役に立てれば

于 2012-12-10T22:59:14.507 に答える