I want to swipe images in windows phone 7. Where do I begin from?
3 Answers
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
-
-
1The 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 Dec 7, 2010 at 9:30
-
2You 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
andLoadPreviousPage
methods.– pho79Mar 15, 2012 at 21:49 -
2@pho79: or check (e.Direction == System.Windows.Controls.Orientation.Horizontal)– giacoderMay 28, 2012 at 8:15
-
1here'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
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/
-
But as far as the documents say..XNA framework is for Gaming..Isn't it?– ShaireenDec 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.– PhillDec 3, 2010 at 5:26
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
}
}
}
}