This article will explain how to work with Touch events
in Silverlight for Windows 7 mobile
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.
are four touch events:
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
From File select New Project. From Silverlight for Windows phone
tab select Windows Phone application project type.
selecting that a new project will get created with below solution structure
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
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.
code I changed is in bigger font and yellow background. Step 4
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.
public partial class MainPage : PhoneApplicationPage
Random radNumber = new Random();
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
TextBlock txtBlck = sender as TextBlock;
Color clr = Color.FromArgb(255, (byte)radNumber.Next(255), (byte)radNumber.Next(255), (byte)radNumber.Next(255));
txtBlck.Foreground = new SolidColorBrush(clr);
txtBlck.FontSize = 50;
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.
when I click text color of the text will get changed. This will act exactly on
the touch in real device.
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.
This article explained how to handle touch in
Silverlight application for Windows 7 mobile. Thanks for reading.