Touch Events in Windows Phone 7

Here we discuss the low-level touch events in Windows Phone 7.

Here we discuss the low-level touch events in Windows Phone 7. In this example when we touch on the TextBlock, it changes its color and text.

For this follow these steps:

Step 1: First we click on: File -> New Project -> Windows Phone Application.

After that we set its name and the location of where we want to save it.

Step 2: After that we create a TextBlock in the ContentPanel like this:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="30" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" />
</Grid>

TchEvWP1.jpg

Step 3: After that we write the following code in the .cs page:

Random random1 = new Random();
Brush brush1;
// Constructor
public MainPage()
{
    InitializeComponent();
    brush1 = textBlock1.Foreground;

    Touch.FrameReported += OnTouchFrameReported;

}

Here we use the Touch.FrameReported event for the low-level touch interface. It does not include any gestures.

Step 4: Now we write the event:

void OnTouchFrameReported(object sender, TouchFrameEventArgs args)
{
    TouchPoint FirstTouchPoint = args.GetPrimaryTouchPoint(null);
    if (FirstTouchPoint != null && FirstTouchPoint.Action == TouchAction.Down)
    {
        if (FirstTouchPoint.TouchDevice.DirectlyOver == textBlock1)
        {
            textBlock1.Text = "Mahak";

            textBlock1.Foreground = new SolidColorBrush(Color.FromArgb(255, (byte)random1.Next(256), (byte)random1.Next(131), (byte)random1.Next(256)));

 

        }

    }

}

Here we use the TouchPoint, it is used to represent the particular finger touching the screen. The Action property, which is basically the type of TouchAction (Down, Move,Up) (Here we use Down). And the DirectyOver property which is used to represent the topmost element which is directly below the finger (it is basically the property of the TouchDevice). And we set the tetxblock Text property and the color of the TextBlock.

The output will be:

TchEvWP2.jpg