Mobile Zone is brought to you in partnership with:

Michael Crump is a Microsoft MVP, INETA Community Champion, and an author of several .NET Framework eBooks. He speaks at a variety of conferences and has written dozens of articles on .NET development. He currently works at Telerik with a focus on our XAML control suite. You can visit his blog at: MichaelCrump.Net or follow him on Twitter at: @mbcrump Michael is a DZone MVB and is not an employee of DZone and has posted 115 posts at DZone. You can read more from them at their website. View Full User Profile

Adding an Easter Egg to your WP7 Application

  • submit to reddit

I was asked the other day how to detect if a user has touched a certain part of the screen. I created some sample code for them but decided that it might make a fun Easter egg as well.  It does this by comparing the X and Y axis of the users current touch point to the location that the “M” exists. Basically, this application will detect if your user is touching the letter M as shown below.



The User Interface

Very simple, just add in the following XAML to the MainPage.xaml :

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock x:Name="txtName" FontSize="56" Foreground="Red" />

I’ve added a TextBlock just to show you the current X and Y coordinates that the user has touched. It will also show you when the user has started and stop touching the device.

The Code

// Constructor
public MainPage()
  Touch.FrameReported += new

void Touch_FrameReported(object sender, TouchFrameEventArgs e)
  TouchPoint mainTouchPoint = e.GetPrimaryTouchPoint(ContentPanel);
  Point position = mainTouchPoint.Position;
  switch (mainTouchPoint.Action)
    case TouchAction.Move:
      txtName.Text = String.Format("x: {0} , y: {1}",position.X, position.Y);
      if (position.X == 20 && position.Y == -132) MessageBox.Show("Secret Hit");
    case TouchAction.Up:
      txtName.Text = "Touch Ended";
    case TouchAction.Down:
      txtName.Text = "Touch Started";          

This code is fairly simple as it just adds an event handler for Touch.FrameReported. Inside the method, we call GetPrimaryTouchPoint and pass it our ContentPanel (You can of course pass it any UI Element). Now that we have our main touch point, we can get the current position the user is touching. The last thing to point out is by calling mainTouchPoint, we can see when the TouchAction is Up, Down or Moving. Pretty powerful stuff with just a few lines of code.

So how did you know the X and Y positions of the letter “M”?

I ran the application and moved the cursor to the position that I wanted the easter egg to occur in, then I added the if…then… statement inside of the TouchAction.Move switch statement.

The Demo

Here you will see a demo of what this project accomplishes. You can download the source code as well.



As you can see from this post, it really just takes a few lines to get this up and running. It will also work even if the user switches screen orientation.  If you have any questions then feel free to contact me. Also, don’t forget to subscribe to my RSS feed and follow me on twitter.


Published at DZone with permission of Michael Crump, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Schlacter Adam replied on Sat, 2012/04/14 - 1:45am

I added an easter egg in the 1800PocketPC app which appears when you go into the "empty cache" settings page & keep on tapping wildly xD

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.