Example of Touch In Windows Phone 7

There are many GestureTypes available, such as: DoubleTap, Hold, FreeDrag, HorizontalDrag etc. In this example we are using the Tap member of the GestureType. Basically Tap means that we have quickly touched and released something. First we look at this in Silverlight and after that in XNA.

In SilverLight: In this program when the user touches a button its foreground color will change. For this use the following procedure.

Step 1: First we click on the File :-> New Project. The following window will appear, here we select this:


Step 2: Now we write the following code in the MainPage.xaml Page like this:

    <Grid x:Name="LayoutRoot" Background="Transparent">
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Example Of Silverlight Tap" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Silverlight Tap" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock x:Name="txt1" Text="My Name is Mahak Gupta"
Padding="0 22"
VerticalAlignment="Center" ManipulationStarted="txt1_ManipulationStarted" />
<Button Content="Click Me!!!!" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />

Here we take a TextBlock and a Button Control. The output will be:


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

Random rand1 = new Random();

// Constructor
public MainPage()
private void txt1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
    Color myclr = Color.FromArgb(255, (byte)rand1.Next(100),
    txt1.Foreground = new SolidColorBrush(myclr);
private void button1_Click(object sender, RoutedEventArgs e)
    Color myclr = Color.FromArgb(255, (byte)rand1.Next(100),
    button1.Foreground = new SolidColorBrush(myclr);

Here we write the code to change the foreground color of both the controls. After that the output will be:


In XNA: In this program we take two normal text values and when the user touches either of them, its color changes. For this use the following:

Step 1: First we click on the File :-> New Project. The following window will appear; here we select this:


Step 2: After that we add a SpriteFont for our program for this we right-click on the Content Part like this:


Here we choose the SpriteFont and use the following code in the SpriteFont1.spritefont:

    <FontName>Segoe UI Mono</FontName> 

Step 3: Now we write the following code in the Game1.cs page:

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch; 
Random rand = new Random();
string text = "Mahak Gupta";
string text1 = "Khyati Zindal";
SpriteFont spritefont1;
Vector2 txtsize1;
Vector2 txtsize;
Vector2 txtpos;
Vector2 txtpos1;
Color txtcolor = Color.Red;
Color txtcolor1 = Color.Plum;
public Game1()
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";

// Frame rate is 30 fps by default for Windows Phone.
    TargetElapsedTime = TimeSpan.FromTicks(333333);

// Extend battery life under lock.
    InactiveSleepTime = TimeSpan.FromSeconds(1);

Here we declare two Text and an instance of SpriteFont and color of the Text. Here Vector2 is used to declare the size and position of the Text.

protected override void LoadContent()
// Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
    spritefont1 = this.Content.Load<SpriteFont>("spritefont1");
    txtsize = spritefont1.MeasureString(text);
    txtsize1 = spritefont1.MeasureString(text1);
    Viewport viewport = this.GraphicsDevice.Viewport;
    txtpos = new Vector2((viewport.Width - txtsize.X) / 2,
    (viewport.Height - txtsize.Y) / 2);
    txtpos1 = new Vector2((viewport.Width - txtsize.X) / 5,
    (viewport.Height - txtsize.Y) / 5);
    TouchPanel.EnabledGestures = GestureType.Tap;

LoadContent is used for load all our content. Here we mention the Size, Position font and GestureType (Tap).

protected override void Update(GameTime gameTime)
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

    // TODO: Add your update logic here
    while (TouchPanel.IsGestureAvailable)
        GestureSample gestureSample = TouchPanel.ReadGesture(); 
        if (gestureSample.GestureType == GestureType.Tap)
            Vector2 touchPosition = gestureSample.Position; 
            if (touchPosition.X >= txtpos.X &&
                touchPosition.X < txtpos.X + txtsize.X &&
                touchPosition.Y >= txtpos.Y &&
                touchPosition.Y < txtpos.Y + txtsize.Y)
                txtcolor = new Color((byte)rand.Next(156),
                txtcolor = Color.Red; 
            if (touchPosition.X >= txtpos1.X &&
                touchPosition.X < txtpos1.X + txtsize1.X &&
                touchPosition.Y >= txtpos1.Y &&
                touchPosition.Y < txtpos1.Y + txtsize1.Y)
                txtcolor1 = new Color((byte)rand.Next(156),
                txtcolor1 = Color.Plum;
protected override void Draw(GameTime gameTime)
    // TODO: Add your drawing code here
    spriteBatch.DrawString(spritefont1, text, txtpos, txtcolor);
    spriteBatch.DrawString(spritefont1, text1, txtpos1, txtcolor1);

The output will be:



Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now