detect long touch (WPF and Microsoft Surface)

2019-08-02 02:34发布

Is there any way I can detect a long touch over a TextBlock (or a Label)?

5条回答
迷人小祖宗
2楼-- · 2019-08-02 03:02

It is possible to do that in an awaitable fashion. Create a timer with specific interval. Start it when user tapped and return the method when timer elapsed. If user release the hand, return the method with false flag.

public static Task<bool> TouchHold(this FrameworkElement element, TimeSpan duration)
{
    DispatcherTimer timer = new DispatcherTimer();
    TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
    timer.Interval = duration;

    MouseButtonEventHandler touchUpHandler = delegate
    {
        timer.Stop();
        if (task.Task.Status == TaskStatus.Running)
        {
            task.SetResult(false);
        }
    };

    element.PreviewMouseUp += touchUpHandler;

    timer.Tick += delegate
    {
        element.PreviewMouseUp -= touchUpHandler;
        timer.Stop();
        task.SetResult(true);
    };

    timer.Start();
    return task.Task;
}

For more information, read this post.

查看更多
小情绪 Triste *
3楼-- · 2019-08-02 03:06

There is an event called TouchAndHoldGesture and PreviewTouchHoldGesture

查看更多
狗以群分
4楼-- · 2019-08-02 03:06

Long touch, or press and hold as I think it's formally named, can be detected through the right click event.
It may be that, if you are using a Surface Window, that the right click event is disabled.

查看更多
对你真心纯属浪费
5楼-- · 2019-08-02 03:09

There is a ContactHoldGesture event on all Surface controls that you can use. But, and I say this as the guy responsible for creating this feature during my time at Microsoft, this event is very poorly designed and should not be used. It doesn't tell you when the system has decided that a finger has moved too much to count as a "hold" and it doesn't give you information needed to draw an animation telling the user that a "hold" is underway. Your much better off doing what @Kevin suggested and building your own implementation.

查看更多
▲ chillily
6楼-- · 2019-08-02 03:20

As far as I know there is no built in way so you would have to do something like this

• Capture the start time on the TouchDown event of the control

• Compare this to the release time in the TouchUup event

• If the two are different by X then run your long touch code

There might be a few things you have to code around but that is the basic idea

查看更多
登录 后发表回答