Touch events in Silverlight for Windows 7 mobile application

Objective

This article will explain how to work with Touch events in Silverlight for Windows 7 mobile application.

Background

On real device touch screen get sensed by users figure. But on the Emulator Mouse movement works as input for touch events. Touch events detect movement of finger on the screen.

There are four touch events

  1. ManipulationStarted
  2. ManipulationInertiaStarting
  3. ManipulationDetla
  4. ManipulationCompleted


In this article, I am going to work with only ManipulationStarted event. Follow the below steps

Step 1

From Start menu select Microsoft Visual Studio 2010 express edition


Step 2

From File select New Project. From Silverlight for Windows phone tab select Windows Phone application project type.


Once selecting that a new project will get created with below solution structure

Make sure in Debug option Windows Phone7 Emulator is selected. If it is selected to Windows Phone 7 Device then Visual studio will deploy the application to mobile device directly.

Step 3

Open MainPage.Xaml and just add a text block. Set the text of the text block as Hello World. Here if you want you can change Title text also.

If you closely look into XAML, you will find there are two Grids inside main Grid. One is title Grid and other is body Grid. So put your text block in the Body grid.

<phoneNavigation:PhoneApplicationPage x:Class=”FirstWindowPhoneApplication.MainPage”

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;

xmlns:phoneNavigation=”clrespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation”

xmlns:d=”http://schemas.microsoft.com/expression/blend/2008&#8243;

xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006&#8243;

mc:Ignorable=”d” d:DesignWidth=”480″ d:DesignHeight=”800″

FontFamily=”{StaticResource PhoneFontFamilyNormal}”

FontSize=”{StaticResource PhoneFontSizeNormal}”

Foreground=”{StaticResource PhoneForegroundBrush}”>

<Grid x:Name=”LayoutRoot” Background=”{StaticResource PhoneBackgroundBrush}”>

<Grid.RowDefinitions>

<RowDefinition Height=”Auto”/>

<RowDefinition Height=”*”/>

</Grid.RowDefinitions>

<Grid x:Name=”TitleGrid” Grid.Row=”0″>

<TextBlock Text=”MY First Mobile Application x:Name=”textBlockPageTitle” Style=”{StaticResource PhoneTextPageTitle1Style}”/>

<TextBlock Text=” Windows 7 x:Name=”textBlockListTitle” Style=”{StaticResource PhoneTextPageTitle2Style}”/>

</Grid>

<Grid x:Name=”ContentGrid” Grid.Row=”1″>


<TextBlock Text=”Hello World” HorizontalAlignment=”Center” VerticalAlignment=”Center” Foreground=”Azure”
ManipulationStarted
=”TextBlock_ManipulationStarted” />

</Grid>

</Grid>

</phoneNavigation:PhoneApplicationPage>

The code I changed is in bigger font and yellow background.

Step 4

In code behind, I am going to write code for changing the color of the text and setting the font size. I will be writing code on event ManipulationStarted. Below is very simple code. In this code I am setting the color to a random and setting the font to double 50.


Step 5

    1 namespace FirstWindowPhoneApplication

    2 {

    3     public partial class MainPage : PhoneApplicationPage

    4     {

    5 

    6         Random radNumber = new Random();

    7         public MainPage()

    8         {

    9             InitializeComponent();

   10 

   11             SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

   12         }

   13 

   14         private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)

   15         {

   16             TextBlock txtBlck = sender as TextBlock;

   17             Color clr = Color.FromArgb(255, (byte)radNumber.Next(255), (byte)radNumber.Next(255), (byte)radNumber.Next(255));

   18             txtBlck.Foreground = new SolidColorBrush(clr);

   19             txtBlck.FontSize = 50;

   20 

   21 

   22         }

   23     }

   24 }

   25 

   26 

   27 

Just press F5 and in Windows 7 mobile emulator you can see the output. In center you can see Hello world. When you click on the text block, font of the text will be changed to 20 and foreground color will change randomly.


Now when I click text color of the text will get changed. This will act exactly on the touch in real device.


But when I click out area of the text box text size and color won’t get to its original because I have not handled ManipulationCompleted event.

Conclusion

This article explained how to handle touch in Silverlight application for Windows 7 mobile. Thanks for reading.

About these ads

One thought on “Touch events in Silverlight for Windows 7 mobile application

  1. Pingback: Monthly Report March 2010: Total Posts 14 « debug mode……

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s