如何在WPF应用程序中检测触摸按住手势?

如何在WPF应用程序中检测触摸按住手势?,wpf,touch,gesture,Wpf,Touch,Gesture,可以通过MouseRightButtonDown事件检测触摸按住手势。不幸的是,直到我将手指从屏幕上松开,它才会启动。太晚了 有人有想法吗?提前感谢。使用提供的保持手势使用提供的保持手势可以以等待的方式进行。创建具有特定间隔的计时器。当用户点击时启动它,当计时器运行时返回该方法。如果用户释放手,则返回带有false标志的方法 public static Task<bool> TouchHold(this FrameworkElement element, TimeSpan durat

可以通过MouseRightButtonDown事件检测触摸按住手势。不幸的是,直到我将手指从屏幕上松开,它才会启动。太晚了


有人有想法吗?提前感谢。

使用

提供的保持手势使用

提供的保持手势可以以等待的方式进行。创建具有特定间隔的计时器。当用户点击时启动它,当计时器运行时返回该方法。如果用户释放手,则返回带有false标志的方法

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;
}
公共静态任务触摸保持(此FrameworkElement元素,时间跨度持续时间)
{
调度程序计时器=新调度程序();
TaskCompletionSource任务=新建TaskCompletionSource();
计时器。间隔=持续时间;
MouseButtonEventHandler touchUpHandler=委托
{
timer.Stop();
if(task.task.Status==TaskStatus.Running)
{
task.SetResult(false);
}
};
element.PreviewMouseUp+=touchUpHandler;
timer.Tick+=委托
{
element.PreviewMouseUp-=touchUpHandler;
timer.Stop();
task.SetResult(true);
};
timer.Start();
返回任务。任务;
}

欲了解更多信息,请阅读此文。

可以以一种期待已久的方式完成此操作。创建具有特定间隔的计时器。当用户点击时启动它,当计时器运行时返回该方法。如果用户释放手,则返回带有false标志的方法

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;
}
公共静态任务触摸保持(此FrameworkElement元素,时间跨度持续时间)
{
调度程序计时器=新调度程序();
TaskCompletionSource任务=新建TaskCompletionSource();
计时器。间隔=持续时间;
MouseButtonEventHandler touchUpHandler=委托
{
timer.Stop();
if(task.task.Status==TaskStatus.Running)
{
task.SetResult(false);
}
};
element.PreviewMouseUp+=touchUpHandler;
timer.Tick+=委托
{
element.PreviewMouseUp-=touchUpHandler;
timer.Stop();
task.SetResult(true);
};
timer.Start();
返回任务。任务;
}

有关更多信息,请阅读此部分。

这段代码很棒。为了完整性,我只添加了一个示例用法:

private async void btn_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
  if (await TouchHold(btn, TimeSpan.FromSeconds(2)))
  {
    // todo: long press code goes here
  }
}
从XAML:

<Button Name="btn" PreviewMouseDown="btn_PreviewMouseDown">Press long</Button>
长按

这段代码很棒。为了完整性,我只添加了一个示例用法:

private async void btn_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
  if (await TouchHold(btn, TimeSpan.FromSeconds(2)))
  {
    // todo: long press code goes here
  }
}
从XAML:

<Button Name="btn" PreviewMouseDown="btn_PreviewMouseDown">Press long</Button>
长按

无论是按钮、标签还是图像,我们都可以使用
MouseDown
MouseUp
启动延迟和停止延迟

对于
MouseDown

    // Declaration of timer and timercount
     int timerCount = 0;
     DispatcherTimer dt = new DispatcherTimer();

     public myConstructor()
            {
                dt.Interval = new TimeSpan(0, 0, 1);
            }

    // Mouse Down Event
     private void EnterHoldState(object sender, TouchEventArgs e)
            {
                timerStarted();
            }

    //Mouse Up event
        private void ExitHoldState(object sender, RoutedEventArgs e)
                {
                    timerStopped();
                }

// Stops the timer and resets the timer count to 0
        private void timerStopped()
        {
            dt.Stop();
            timerCount = 0;
        }

// Starts the timer and sets delayCounter function for counting the delay seconds and acts on it
        private void timerStarted()
        {
            dt.Tick += delayCounter;
            dt.Start();
            
        }

//Once delay timer reaches 2 seconds, the button navigates to nextpage.
        private void delayCounter(object sender, EventArgs e)
        {
            timerCount++;
            if (timerCount == 2)
            {
                this.NavigationService.Navigate(new nextPage());
            }
        }

无论是按钮、标签还是图像,我们都可以使用
MouseDown
MouseUp
启动延迟和停止延迟

对于
MouseDown

    // Declaration of timer and timercount
     int timerCount = 0;
     DispatcherTimer dt = new DispatcherTimer();

     public myConstructor()
            {
                dt.Interval = new TimeSpan(0, 0, 1);
            }

    // Mouse Down Event
     private void EnterHoldState(object sender, TouchEventArgs e)
            {
                timerStarted();
            }

    //Mouse Up event
        private void ExitHoldState(object sender, RoutedEventArgs e)
                {
                    timerStopped();
                }

// Stops the timer and resets the timer count to 0
        private void timerStopped()
        {
            dt.Stop();
            timerCount = 0;
        }

// Starts the timer and sets delayCounter function for counting the delay seconds and acts on it
        private void timerStarted()
        {
            dt.Tick += delayCounter;
            dt.Start();
            
        }

//Once delay timer reaches 2 seconds, the button navigates to nextpage.
        private void delayCounter(object sender, EventArgs e)
        {
            timerCount++;
            if (timerCount == 2)
            {
                this.NavigationService.Navigate(new nextPage());
            }
        }

你好,很好,谢谢。但我看到了两个问题。1) element.PreviewMouseUp-=touchUpHandler;应在touchUpHandler中调用。否则,每次短按都会向元素添加一个新的事件处理程序,并且不会调用分离代码。2) 正在检查TaskStatus。Running将永远不会为true。TaskCompletionSource中的任务将始终处于等待激活状态。检查此项或检查是否不存在任何最终状态(已取消、故障、RANTO完成)。很好,很好,谢谢你。但我看到了两个问题。1) element.PreviewMouseUp-=touchUpHandler;应在touchUpHandler中调用。否则,每次短按都会向元素添加一个新的事件处理程序,并且不会调用分离代码。2) 正在检查TaskStatus。Running将永远不会为true。TaskCompletionSource中的任务将始终处于等待激活状态。检查此项或检查是否不存在任何最终状态(已取消、故障、RANTO完成)。问候