开发者

How do I detect when toolkit:GestureListener Hold has stopped?

Is there a way I can detect thi开发者_如何转开发s? I want to keep performing an action as long as the user is holding on an icon.


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



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.


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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜