Touch events in Silverlight for Windows Phone 7 Application Part 02

No.of Views667
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  21 Apr 2010 09:04:03
Tag : Windows Phone , Utilities
This article will explain how to work with Touch events in Silverlight for Windows 7 mobile application
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will explain how to work with Touch events in Silverlight for Windows 7 mobile application.  This is part 2 of the touch events. Read my first article on touch events  before going through this article.

Background

We saw in last article on touch events that, even if after touching (clicking) somewhere else than text block, the text in text block was not resuming to original fonts and colors.  The reason behind that was, I attached the event to the text block. I can conclude reason as follows
1.    I attached ManipulationStarted event to text block.
2.    I did not handle ManipulationCompleted event for the text block.
3.    In ManipulationStarted event for text block I changed the text color and font size.
4.    But I did not write code for resuming the original font size and color on ManipulationCompleted event for text block.

Normal behavior should be, when user is touching or clicking somewhere else on the screen text block should resume to its originality.

So how to achieve it

 Fine; when we see the class hierarchy. I will explain this hierarchy in later articles. But what point to be noted here is that , we can override OnManipulationStarted method and then can  sense  the touch on whole screen.  We can override this method and check if the source of the touch is text block , we can manipulate color and font . If source  is somewhere on the screen we can  restore the default font and color.
 

 

Image Loading

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

 

Image Loading

Step 2

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

 

Image Loading

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

 

Image Loading


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.
 

Image Loading

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"      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation"      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"      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>            <!--TitleGrid is the name of the application and page title-->          <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>            <!--ContentGrid is empty. Place new content here-->          <Grid x:Name="ContentGrid" Grid.Row="1">              <TextBlock x:Name="txtHelloWord" Text="Hello World" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Azure" />             </Grid>      </Grid>  </phoneNavigation:PhoneApplicationPage>

The code I changed is in bigger font and yellow background.  This time there is no event attached to text block.

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 overridden method.

MainPage.Xaml.Cs
 

namespace FirstWindowPhoneApplication  {      public partial class MainPage : PhoneApplicationPage      {            Random radNumber = new Random();          public MainPage()          {              InitializeComponent();                SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;          }                        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)          {                Color clr = Colors.White;              double  font = 30;               if (e.OriginalSource == txtHelloWord)              {                   clr = Color.FromArgb(255, (byte)radNumber.Next(255), (byte)radNumber.Next(255), (byte)radNumber.Next(255));                   font = 75;               }                txtHelloWord.Foreground = new SolidColorBrush(clr);              txtHelloWord.FontSize = font;               base.OnManipulationStarted(e);          }      }  }

Explanation

1.    I am overriding OnManipulationStarted method here.
2.    I am checking what the source of calling the method is.
3.    If it is text box, I am setting the font size and color to random color.
4.    If it is not text box , I am setting size and color to default.


Step 5

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 75  and foreground color will change randomly.  But when you click outside the text block or any other place  on page text  font size will be changed to  30 and text  color to  white.
 

 

Image Loading

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

 

Image Loading

Again when user will touch  any other place on the page , text color and text size will restore to white and 30 respectively.

 

Image Loading

Conclusion

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

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
By:replica breitling navitimerDate Of Posted:5/18/2012 2:54:17 AM
Classic Breitling Navitimer Replica, Replica Breitling Navitimer For Sale
The way you described this is very good. I am really glad to [url=http://www.demontres.fr/replique-tag-heuer-carrera-cb528.html]read more[/url]
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top