WPF是否具有触摸和保持手势? [英] Do WPF have Touch-and-Hold gesture?

查看:84
本文介绍了WPF是否具有触摸和保持手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF是否具有触摸和保持手势?我找不到相应的事件,因此我尝试自己实施一个.我知道有Stylus类,但是在WPF中却无济于事.如果没有,那是我的代码:

Do WPF have Touch-and-Hold gesture? I cannot find event for that, so I tried to implement one for myself. I know that there is Stylus class but in WPF it does not help me. If there aren't one there is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace WebControlTouch
{
    /// <summary>
    /// Due to lack of Touch-and-Hold gesture, here is implementation of it. Stupid M$.
    /// </summary>
    public static class Touch_and_Hold
    {
        #region Constructor + methods
        /// <summary>
        /// Static constructor which creates timer object with 1000ms interval, also sets parameters of Timer.
        /// </summary>
        static Touch_and_Hold()
        {
            gestureTimer = new Timer(1000);
            gestureTimer.AutoReset = false;
            gestureTimer.Elapsed += gestureTimer_Elapsed;
        }
        /// <summary>
        /// On elasped (time ofc)
        /// </summary>
        /// <seealso cref="gestureTimer"/>        
        static void gestureTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            occured = true;
        }
        /// <summary>
        /// Call it on OnTouchDown event.
        /// It will start timer and will count time of touch
        /// </summary>
        /// <returns>Returns that gesture occured</returns>
        public static void onTouch()
        {
            gestureTimer.Start();
        }

        /// <summary>
        /// Call it on touch up mainwindow event (or somewhere else)
        /// It stops gesture timer
        /// </summary>
        public static void onTouchUp()
        {
        occured = false;
        }
        #endregion 
        #region Members + properties
        /// <summary>
        /// Timer for measuring touchTime
        /// </summary>
        private static Timer gestureTimer;
        /// <summary>
        /// Do tap-and-hold occured
        /// </summary>
        private static bool occured = false;
        /// <summary>
        /// Property for getting occured flag
        /// </summary>
        public static bool occuredGesture
        {
            get { return occured; }
        }
        #endregion
    }

}

如果是,请告诉我活动名称.如果没有,请尝试引导我解决. 任何帮助将不胜感激.

If yes, please tell me name of the event. If not - try to steer me to solution. Any help will be very appreciated.

推荐答案

我以前通过创建一个自定义控件来实现此目的,该控件扩展了按钮以在按下并保持延迟后延迟按钮命令的触发. /p>

I've previously achieved this by create a custom control that extends button to delay the trigger of a button command after a delay on press-and-hold.

public class DelayedActionCommandButton : Button

第一个依赖项属性:

public static readonly DependencyProperty DelayElapsedProperty =
        DependencyProperty.Register("DelayElapsed", typeof(double), typeof(DelayedActionCommandButton), new PropertyMetadata(0d));

public static readonly DependencyProperty DelayMillisecondsProperty =
        DependencyProperty.Register("DelayMilliseconds", typeof(int), typeof(DelayedActionCommandButton), new PropertyMetadata(1000));

public double DelayElapsed
    {
        get { return (double)this.GetValue(DelayElapsedProperty); }
        set { this.SetValue(DelayElapsedProperty, value); }
    }

    public int DelayMilliseconds
    {
        get { return (int)this.GetValue(DelayMillisecondsProperty); }
        set { this.SetValue(DelayMillisecondsProperty, value); }
    }

这些使我们可以控制延迟的时间以及剩余时间的输出.

These give us a control on how the delay should be and an output of how long is left.

接下来,我创建一个动画,以控制播放完成时触发命令的经过量.还有一个取消延迟方法:

Next I create an animation, to control the elapsed amount which when complete fires the command. There is also a cancel delay method:

    private void BeginDelay()
    {
        this._animation = new DoubleAnimationUsingKeyFrames() { FillBehavior = FillBehavior.Stop };
        this._animation.KeyFrames.Add(new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), new CubicEase() { EasingMode = EasingMode.EaseIn }));
        this._animation.KeyFrames.Add(new EasingDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(this.DelayMilliseconds)), new CubicEase() { EasingMode = EasingMode.EaseIn }));
        this._animation.Completed += (o, e) =>
        {
            this.DelayElapsed = 0d;
            this.Command.Execute(this.CommandParameter);    // Replace with whatever action you want to perform
        };

        this.BeginAnimation(DelayElapsedProperty, this._animation);
    }

    private void CancelDelay()
    {
        // Cancel animation
        this.BeginAnimation(DelayElapsedProperty, null);
    }

最后,我们连接事件处理程序:

Finally, we wire up the event handlers:

private void DelayedActionCommandButton_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
    {
        this.BeginDelay();
    }

    private void DelayedActionCommandButton_TouchUp(object sender, System.Windows.Input.TouchEventArgs e)
    {
        this.CancelDelay();
    }

在XAML中使用时,您可以选择创建一个模板,该模板可以根据DelayElapsed的值进行动画处理,以提供倒计时或视觉提示(例如边框扩展),无论您喜欢什么.

When used in XAML, you can optionally create a template that can animate based on the value of DelayElapsed to provide a countdown, or visual cue such as an expanding border, whatever takes your fancy.

这篇关于WPF是否具有触摸和保持手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆