21

I want to swipe images in windows phone 7. Where do I begin from?

2
  • What does "swipe"ing an image mean? Dec 3, 2010 at 4:16
  • By swipe I meant a gesture where touch and drag and leave will kind of flip the page
    – Shaireen
    Dec 3, 2010 at 4:20

3 Answers 3

41

You can use the GestureService in the Silverlight Control Toolkit for Windows Phone 7. In your UI element, add the following piece of code (after you have referenced the toolkit's DLL in your WP7 project) -

<toolkit:GestureService.GestureListener>
    <toolkit:GestureListener Flick="OnFlick"/>
</toolkit:GestureService.GestureListener>  

Implement the handler OnFlick in the code-behind file, like so -

private void OnFlick(object sender, FlickGestureEventArgs e)
{
   var vm = DataContext as SelectedCatalogViewModel;
   if (vm != null)
   {
      // User flicked towards left
      if (e.HorizontalVelocity < 0)
      {
         // Load the next image 
         LoadNextPage(null);
      }

      // User flicked towards right
      if (e.HorizontalVelocity > 0)
      {
         // Load the previous image
         LoadPreviousPage();
      }
   }
}

Hope this helps, indyfromoz

6
  • What does "var vm = DataContext as SelectedCatalogViewModel;" mean?
    – Shaireen
    Dec 7, 2010 at 4:34
  • 1
    The page that requires a swipe gesture implemented is bound to a ViewModel. The ViewModel class is SelectedCatalogViewModel and I am using MVVMLight's (MVVM toolkit) RelayCommand to wire up events from the code-behind with the underlying ViewModel. Sorry about the confusion. Cheers, indyfromoz
    – indyfromoz
    Dec 7, 2010 at 9:30
  • 2
    You might also want to make sure that the horizontal velocity is greater (by an order of magnitude, even) than the vertical velocity as well, so that diagonal or mostly vertical swipes don't trigger the LoadNextPage and LoadPreviousPage methods.
    – pho79
    Mar 15, 2012 at 21:49
  • 2
    @pho79: or check (e.Direction == System.Windows.Controls.Orientation.Horizontal)
    – giacoder
    May 28, 2012 at 8:15
  • 1
    here's the part you need in your xaml: xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" Oct 29, 2012 at 21:20
7

If you don't want to use the silverlight toolkit you can use the XNA framework.

http://www.nickharris.net/2010/11/using-touchpanel-for-gestures-in-windows-phone-7/

2
  • But as far as the documents say..XNA framework is for Gaming..Isn't it?
    – Shaireen
    Dec 3, 2010 at 5:20
  • Yes, but the XNA framework works on WP7 for WP7 gaming, so you can reference the gestures part of the framework for a normal application just fine. Means you don't have to rely on 3rd party libraries.
    – Phill
    Dec 3, 2010 at 5:26
5

Try this:

using Microsoft.Phone.Controls;

public partial class MyControl
{
    public MyControl()
    {
        InitializeComponent();

        var gl = GestureService.GetGestureListener(asd);
        gl.Flick += new EventHandler<FlickGestureEventArgs>(GestureListener_Flick);
    }

    private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
    {
        if (e.Direction == Orientation.Horizontal)
        {
            if (e.HorizontalVelocity < 0) // determine direction (Right > 0)
            {
                //Some Action
            }
            else
            {
                //Some Action
            }
        }
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.