The following example shows how to change the color of a Border
control by responding to the Touch.FrameReported
event. Within the TouchPointView
XAML file is a named Border
control as shown:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Border x:Name="border"
Height="100"
Width="200"
Background="{StaticResource PhoneAccentBrush}" />
</Grid>
The code-beside file, TouchPointView.xaml.cs, subscribes to the Touch.FrameReported
event within the page constructor. The event handler retrieves the primary touch point, and if the TouchPoint
is above the Border
, the Border
object’s Background
is switched (see Listing 12.2).
LISTING 12.2. TouchPointView
Class
public partial class TouchPointView : PhoneApplicationPage
{
readonly SolidColorBrush directlyOverBrush
= new SolidColorBrush(Colors.Orange);
readonly Brush normalBrush;
public TouchPointView()
{
InitializeComponent();
normalBrush = border.Background;
Touch.FrameReported += HandleFrameReported;
}
void HandleFrameReported(object sender, TouchFrameEventArgs e)
{
TouchPoint primaryTouchPoint = e.GetPrimaryTouchPoint(null);
if (primaryTouchPoint == null
|| primaryTouchPoint.TouchDevice.DirectlyOver != border)
{
return;
}
if (primaryTouchPoint.Action == TouchAction.Down)
{ border.Background = directlyOverBrush;
}
else
{
border.Background = normalBrush;
}
}
}
Using the Touch
and TouchPoint
API provides you with the ability to respond to touch at a very low level. Subsequent sections of this chapter look at two higher-level abstractions of the Touch
and TouchPoint
APIs—namely, manipulation events and the Windows Phone Toolkit, beginning with manipulation events.
52.15.57.3