如何检测 toolkit:GestureListener Hold 何时停止?

发布于 2024-11-02 21:41:01 字数 40 浏览 1 评论 0原文

有什么方法可以检测到这一点吗?我想只要用户按住图标就继续执行操作。

Is there a way I can detect this? I want to keep performing an action as long as the user is holding on an icon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半边脸i 2024-11-09 21:41:01

您可以使用鼠标操作事件来检测执行操作的时间,而不是使用 GestureListener。例如:

  • 监听 MouseLeftButtonDown 以了解用户何时触摸了图标
  • 继续执行操作,直到 MouseLeftButtonUpMouseLeave 触发,表明用户触摸了图标不再触摸该图标
  • 您可能还需要使用 MouseEnter 来启动操作

Instead of using the GestureListener for this you could instead use the mouse manipulation events to detect how long to perform your action. For instance:

  • Listen for MouseLeftButtonDown to know when the user has touched the icon
  • Keep performing the action until either MouseLeftButtonUp or MouseLeave fire indicating that the user is no longer touching that icon
  • You may also have to play with MouseEnter for initiating the action
孤云独去闲 2024-11-09 21:41:01

今天我在我的项目中做了同样的事情。我会告诉你我实现的基本逻辑(假设它必须在按钮上完成)。
步骤1:

按钮_ManipulationStarted_事件上启动一个计时器,在间隔之后,您可以想要触发重复动作。


第 2 步:

按钮上_ManipulationCompleted_事件停止计时器。

第 3 步:

如果计时器被触发,请停止计时器并启动另一个计时器,其中间隔 = 操作的重复间隔< /strong>。并且在第二个计时器 fire 处理程序中,仅当 control 具有焦点时才执行您的操作。在这种情况下,控件是一个按钮,您可以检查button.IsPressed是否为true,然后执行操作。


代码看起来像:


Button forward=new Button();
DispatcherTimer forwardHoldTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
forward.ManipulationStarted += (a, b) => { forwardHoldTimer.Start(); };
forward.ManipulationCompleted += (c, d) => { forwardHoldTimer.Stop(); };
forwardHoldTimer.Tick+=(s1,e1)=>
{
        forwardHoldTimer.Stop();
        DispatcherTimer t = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(100) };
        t.Tick += (x, y) =>
        {
             if (forward.IsPressed)
             {
                  //Your action logic will go here
             }
             else
                t.Stop();
         };
         t.Start();
};

希望这有帮助。

Today only i did the same thing in my project.I'll tell you the basic logic what i implemented(assuming it has to be done on button).
Step 1:

On the button _ManipulationStarted_ event start a timer with the interval after which you want to fire the repeat action.


Step 2:

On the button _ManipulationCompleted_ event stop the timer.

Step 3:

If the timer is fired,stop the timer and start another timer with interval = the repeat interval for your action.And inside the second timer fire handler perform your operation only if the control has focus. In this case, where the control is a button, you can check if the button.IsPressed is true then perform action.


The code will look something like:


Button forward=new Button();
DispatcherTimer forwardHoldTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
forward.ManipulationStarted += (a, b) => { forwardHoldTimer.Start(); };
forward.ManipulationCompleted += (c, d) => { forwardHoldTimer.Stop(); };
forwardHoldTimer.Tick+=(s1,e1)=>
{
        forwardHoldTimer.Stop();
        DispatcherTimer t = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(100) };
        t.Tick += (x, y) =>
        {
             if (forward.IsPressed)
             {
                  //Your action logic will go here
             }
             else
                t.Stop();
         };
         t.Start();
};

Hope this helps.

桃扇骨 2024-11-09 21:41:01

注意:Amresh Kumar 建议使用操纵事件是正确的。另外,我在 Windows Phone 应用中心论坛上得到了相同的建议,因此我编辑了这篇文章以反映代码更改。

之前,UX 很不稳定,因为将手指从屏幕上抬起并不总是会触发取消。毫不奇怪,工具包中的 GestureCompleted 代码似乎比鼠标按钮事件更适合触摸屏。

XAML:

<iconControls:iconUpDownArrow>
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Tap="RangeUpTap" Hold="RangeUpHold" GestureCompleted="RangeUpCancel" />
    </toolkit:GestureService.GestureListener>
</iconControls:iconUpDownArrow>

代码:

private void RangeUpTap(object sender, GestureEventArgs e)
{
    RangeIncrementUp(sender, e);
}

private readonly TimeSpan _rangeIncrementTimeSpan = new TimeSpan(1500000);
private readonly DispatcherTimer _rangeIncrementTimer = new DispatcherTimer();

private void RangeUpHold(object sender, GestureEventArgs e)
{
    _rangeIncrementTimer.Interval = _rangeIncrementTimeSpan;

    _rangeIncrementTimer.Tick += RangeIncrementUp;

    _rangeIncrementTimer.Start();
}

private void RangeUpCancel(object sender, GestureEventArgs e)
{
    _rangeIncrementTimer.Stop();

    _rangeIncrementTimer.Tick -= RangeIncrementUp;
}

private void RangeIncrementUp(object sender, EventArgs e)
{
    int range = Convert.ToInt32(tBoxRange.Text);

    if (range < 1000)
    {
        range += 10;
    }

    tBoxRange.Text = range.ToString();
}

NOTE: Amresh Kumar was correct in suggesting using the manipulation events. Also, I was given the same advice on the Windows Phone App Hubs forums so I've edited this post to reflect the code changes.

Before, the UX was flaky because lifting my finger off the screen didn't always trigger a cancellation. Not surprisingly, the GestureCompleted code in the toolkit appears to be better geared towards touchscreens than are mouse button events.

XAML:

<iconControls:iconUpDownArrow>
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Tap="RangeUpTap" Hold="RangeUpHold" GestureCompleted="RangeUpCancel" />
    </toolkit:GestureService.GestureListener>
</iconControls:iconUpDownArrow>

code:

private void RangeUpTap(object sender, GestureEventArgs e)
{
    RangeIncrementUp(sender, e);
}

private readonly TimeSpan _rangeIncrementTimeSpan = new TimeSpan(1500000);
private readonly DispatcherTimer _rangeIncrementTimer = new DispatcherTimer();

private void RangeUpHold(object sender, GestureEventArgs e)
{
    _rangeIncrementTimer.Interval = _rangeIncrementTimeSpan;

    _rangeIncrementTimer.Tick += RangeIncrementUp;

    _rangeIncrementTimer.Start();
}

private void RangeUpCancel(object sender, GestureEventArgs e)
{
    _rangeIncrementTimer.Stop();

    _rangeIncrementTimer.Tick -= RangeIncrementUp;
}

private void RangeIncrementUp(object sender, EventArgs e)
{
    int range = Convert.ToInt32(tBoxRange.Text);

    if (range < 1000)
    {
        range += 10;
    }

    tBoxRange.Text = range.ToString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文