This chapter is taken from book "Programming Windows Phone 7" by Charles Petzold published by Microsoft press. http://www.charlespetzold.com/phone/index.html Windows Phone 7 comes with a feature that is likely to be new and unusual. The screen on the phone is sensitive to touch. The multi-touch screen on a Windows Phone 7 device can detect at least four simultaneous fingers. It is the interaction of these fingers that makes multi-touch so challenging for programmers. In a Silverlight program, touch input is obtained through events. In an XNA program, touch input comes through a static class polled during the Update method. One of the primary purposes of the XNA Update method is to check the state of touch input and make changes that affect what goes out to the screen during the Draw method. The multi-touch input device is referred to in XNA as a touch panel. It is possible to obtain information about the multi-touch device itself by calling the static TouchPanel.GetCapabilities method. The TouchPanelCapabilities object returned from this method has two properties:
TouchCollection touchLocations = TouchPanel.GetState(); The TouchCollection is a collection of zero or more TouchLocation objects. TouchLocation has three properties:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Name="txtblk" Text="Hello, Windows Phone 7!" Padding="0 22" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid> I used Padding rather than Margin because Padding is space inside the TextBlock. The TextBlock actually becomes larger than the text size would imply. Here's the complete code-behind file. The constructor of MainPage installs the Touch.FrameReported event handler. using System;using System.Windows.Input;using System.Windows.Media;using Microsoft.Phone.Controls; namespace SilverlightTouchHello{ public partial class MainPage : PhoneApplicationPage { Random rand = new Random(); Brush originalBrush; public MainPage() { InitializeComponent(); originalBrush = txtblk.Foreground; Touch.FrameReported += OnTouchFrameReported; } void OnTouchFrameReported(object sender, TouchFrameEventArgs args) { TouchPoint primaryTouchPoint = args.GetPrimaryTouchPoint(null); if (primaryTouchPoint != null && primaryTouchPoint.Action == TouchAction.Down) { if (primaryTouchPoint.TouchDevice.DirectlyOver == txtblk) { txtblk.Foreground = new SolidColorBrush( Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256))); } else { txtblk.Foreground = originalBrush; } } } }} The event handler is only interested in primary touch points with an Action of Down. If the DirectlyOver property is the element named txtblk, a random color is created. Summary I hope this article helps you to clear the touch interface programming for windows phone 7.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: