Search code examples
windows-phone-7silverlight-toolkitgesture

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


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.


Solution

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