Reputation: 1705
I'm currently trying to find a MVVMLight compatible way to use gestures in my WP8 app. Specifically I just want to detect a swipe/flick and bind it to a RelayCommand in my view model. Has there been any recent solution developed over the years that I'm unaware of?
I've done some research before hand, and the results I've come up with are mostly outdated or no longer exist. i.e:
toolkit:GestureListener
from the Windows Phone Toolkit supports gestures but requires you to couple the ViewModel with the View.Note: Found out that toolkit:GestureListener
has been deprecated.
Upvotes: 0
Views: 675
Reputation: 1705
Found the answer to my question.
Instead of using toolkit:GestureListener
, I found out that EventToCommand
with ManipulationDelta
or ManipulationCompleted
works as well:
<i:Interaction.Triggers>
<i:EventTrigger EventName="ManipulationDelta">
<Command:EventToCommand Command="{Binding SlideOutDeltaCommand, Mode=OneWay}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
<i:EventTrigger EventName="ManipulationCompleted">
<Command:EventToCommand Command="{Binding SlideOutCompletedCommand, Mode=OneWay}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
By passing in EventArgs to the ViewModel, you can detect whether a swipe gesture has been issued:
public RelayCommand<ManipulationDeltaEventArgs> SlideOutDeltaCommand
{
get;
private set;
}
private void OnSlideDelta(ManipulationDeltaEventArgs e)
{
var delta = e.CumulativeManipulation.Translation;
//If Change in X > Change in Y, its considered a horizontal swipe
var isDeltaHorizontal = Math.Abs(delta.X) > Math.Abs(delta.Y) ? true : false;
}
public MainViewModel()
{
SlideOutDeltaCommand = new RelayCommand<ManipulationDeltaEventArgs>((e) => OnSlideDelta(e));
}
Upvotes: 0
Reputation: 6142
Joost Van Schaaik created such a behaviour on wp7: http://dotnetbyexample.blogspot.be/2011/03/simple-windows-phone-7-silverlight.html
He can be contacted on twitter by @localjoost
Upvotes: 1