Silverlight for Windows Phone 7 is version 3.7 hence a WP7 control should be easily used within a standard Silverlight 4 application. But that’s in theory. The major differences between these two worlds are the so called “Manipulation” events. Each UIElement in WP7’s Silverlight is “enabled” with the following routed events:
Generally this sequence of events encapsulates one or more touch gestures in a transaction block. The event arguments carry all the information needed to determine what the gesture is and what effect to generate. And of course RadControl for WP7 use these events extensively in order to provide consistent and rich user experience throughout the entire framework.
But what about running these controls in a standard Silverlight Application where the manipulation events are not present? The solution is to generate the gestures manually, depending on mouse input – meet the ManipulationBehavior class we have created to enable standard single-touch gestures outside the WP7 context. The behavior itself attaches to a valid UIElement instance and hooks its MouseLeftButtonDown, MouseMove, MouseLeftButtonUp and MouseLeave events. When mouse input is received, the behavior generates the appropriate gestures and raises one or more of the following events:
In order to determine whether a flick gesture should be generated we calculate the timespan between the last mouse move and the MouseLeftButtonUp event:
protected
virtual
void
HandleMouseLeftButtonUp(MouseButtonEventArgs e)
{
Point current = e.GetPosition(Application.Current.RootVisual);
if
(current ==
this
.manipulationStartPosition)
{
ManipulationTapEventArgs args =
new
ManipulationTapEventArgs(e.OriginalSource, current);
this
.OnTap(args);
}
else
if
(DateTime.Now.TimeOfDay.TotalMilliseconds -
this
.lastMoveTime.TotalMilliseconds <= FlickTimeSpan)
{
// less than 10 milliseconds between a mousemove and left button release is considered as a Flick gesture
double
velocity =
this
.CalculateFlickVelocity(e.GetPosition(Application.Current.RootVisual));
ManipulationFlickEventArgs args =
new
ManipulationFlickEventArgs(e.OriginalSource, velocity);
this
.OnFlick(args);
}
this
.OnManipulationCompleted();
}
And here is how the velocity is calculated:
private
double
CalculateFlickVelocity(Point currentPosition)
{
double
offsetY = currentPosition.Y -
this
.manipulationStartPosition.Y;
double
milliseconds = DateTime.Now.TimeOfDay.TotalMilliseconds -
this
.manipulationStartTime.TotalMilliseconds;
// speed (pixels per millisecond)
double
panSpeed = offsetY / milliseconds;
double
height =
this
.associatedElement.RenderSize.Height;
if
(panSpeed == 0)
{
return
height / 2;
}
return
panSpeed * height * 1.5d;
}
Well, enough words, you’d better see it for yourself (this is not just a picture :)
Please note that the text here looks a bit different than in WP7 because the Segoe WP Font is installed with the Windows Phone 7 tools and we have intentionally specified the standard Segoe UI Font for the demo.
Do not hesitate to read more about our RadControls for WP7 and to grab a copy of our fresh CTP release. Any feedback is most welcome.
Georgi worked at Progress Telerik to build a product that added the Progress value into the augmented and virtual reality development workflow.