Programming_Windows_Phone_7 读书笔记—基础篇(触控)

201103031529482441.jpg
作品目标:Windows Phone 7 开发技术实用手册
Programming_Windows_Phone_7 读书笔记规划
  • 基础篇
    基础篇是Silvelight和XNA共有的部分,分为
    • 横向和纵向视图
    • 触控
    • Sensors和服务(加速器、Bing Map、云计算)
    • 全球化
    • 应用设计问题集锦
  • Silverlight篇
    结合《Programming_Windows_Phone_7》与Windows Phone Silverlight动手实验程序,分为
    • 元素和属性
    • 复杂的布局
    • 应用程序菜单栏
    • 数据绑定和项的控制
    • 动画和多媒体
    • 数据透视和全景
  • XNA篇
    结合《Programming_Windows_Phone_7》与Windows Phone XNA框架动手实验程序。
    • 运动的规则
    • 2D游戏中实现触摸和手势
    • 2D游戏中实现消息发送和墓碑机制
    • 3D游戏的触控和基本3D物理学
参考资料:
读书笔记的内容源于也多于 Programming_Windows_Phone_7,是Windows Phone 7开发技术学习的合集。规划的章节若有不合理之处,日后将修改。
虽然大部分内容看起来都似曾相识,也许别人都写过。在整理时,力求从深度和不同的侧面诠释Windows Phone的开发技术。由于个人能力和水平有待提高,很多问题的分析还很肤浅。敬请各位不吝赐教,提出改进建议。
 
 
 
目录
 
 
触控的输入方式是Windows Phone7的核心功能,程序开发应用Windows Phone 7的多种触控方式,提供使用使用者更多难忘的探索期望和乐趣无穷的交互式体验。例如,拖拽照片或者滑动手指轻轻翻页,而多点触控则提供更多难以想象的功能。
触控键盘声音:用户在触控Windows Phone 7的虚拟键盘时,会获得不一样的体验。Windows Phone 7系统循环发出8种不同的声音,就像从远处传来的脚步声,虽然相识却不同,以此自然的效果来减少用户重复按键的"焦虑"。
Silverlight和XNA对于触控的处理却不尽然相同,Silverlight的触控通过捕获事件的方式实现的,在XNA程序中触控是通过静态类的循环周期的轮询实现的。XNA Update方法的主要目的就是检查触控的状态,并将其响应效果通过Draw方法反应在屏幕上。
 

XNA的简单触控处理

XNA作为多点触摸输入设备的触摸屏,是使用静态的TouchPanel类的方法来获得触控的输入。
通过调用静态TouchPanel.GetCapabilities方法对多点触摸设备本身的信息。
TouchPanel.Capabilities返回的对象有两个属性: 
• IsConnected:如果触摸屏可用。对于Windows Phone,这个值为True 
• MaximumTouchCount:返回触摸点的数量,Windows Phone至少有4个。 
在大多数情况下,需要使用其他两个的TouchPan el静态方法: TouchPanel.GetState()和TouchPanel.ReadGesture ()。
TouchPanel的触摸输入处理是在Update方法里,在XnaTouchHello示例中: 
TouchCollection touchLocations = TouchPanel.GetState(); 
foreach (TouchLocation touchLocation in touchLocations)
{
if (touchLocation.State == TouchLocationState.Pressed)
{
Vector2 touchPosition = touchLocation.Position;
 
if (touchPosition.X >= textPosition.X &&
touchPosition.X < textPosition.X + textSize.X &&
touchPosition.Y >= textPosition.Y &&
touchPosition.Y < textPosition.Y + textSize.Y)
{
textColor = new Color((byte)rand.Next(256),
(byte)rand.Next(256),
(byte)rand.Next(256));
}
else
{
textColor = Color.White;
}
}
}
上面的代码实现:如果触控的位置在文本显示的范围内改变字体的颜色;如果触控的位置在文本显示的范围之外,则文本字体的颜色显示为白色。
其中TouchCollection是零或多个TouchLocation对象的集合。 
 
TouchCollection
 
Provides methods and properties for accessing state information for the touch screen of a touch-enabled device.
Name
Description
Gets the current number of touch locations for the touch screen.
Indicates if the touch screen is available for use.
Determines if the touch location array is read-only.
Gets or sets the information of the specified touch location.
TouchLocation
Provides methods and properties for interacting with a touch location on a touch screen device.
Name
Description
Id
Gets the ID of the touch location.
Gets the position of the touch location.
Gets the state of the touch location.
TouchLocation 具有TryGetPreviousLocation 方法,其用法是
TouchLocation previousTouchLocation;
bool success = touchLocation.TryGetPreviousLocation(out previousTouchLocation);
TouchLocation.TryGetPreviousLocation Method:Attempts to get the previous location of this touch location object.
touchLocation.State发生了移动,调用TouchLocation.TryGetPreviousLocation 方法,可以获得以前的位置,并计算与当前位置以后的差异。
如果touchLocation.State处于Pressed,状态,TryGetPreviousLocation将返回falsepreviousTouchLocation.State将等于枚举成员TouchLocationState.Invalid

XNA的手势识别

TouchPanel 类包含手势识别,在 XnaTapHello工程中的 LoadContentXnaTouchHello的不同之处在于:
TouchPanel.EnabledGestures = GestureType.Tap;
Update的处理方式也大不相同。
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
 
while (TouchPanel.IsGestureAvailable)
{
GestureSample gestureSample = TouchPanel.ReadGesture();
 
if (gestureSample.GestureType == GestureType.Tap)
{
Vector2 touchPosition = gestureSample.Position;
 
if (touchPosition.X >= textPosition.X &&
touchPosition.X < textPosition.X + textSize.X &&
touchPosition.Y >= textPosition.Y &&
touchPosition.Y < textPosition.Y + textSize.Y)
{
textColor = new Color((byte)rand.Next(256),
(byte)rand.Next(256),
(byte)rand.Next(256));
}
else
{
textColor = Color.White;
}
}
}
上面的代码中只判断了一种手势,当Tap(手指单击)手势发生时,通过 TouchPanel .ReadGesture()取得 GestureSample
GestureSample gestureSample = TouchPanel.ReadGesture();
GestureSamplePosition 属性,判断手指单击的位置在文本区域之内还是文本区域之外。
 
 

Silverlight的简单触控

Silverlight支持多点触控,与XNA一样对于手指触控的检测也分为low-level和high-level触控。
实现 Silverlight low-level( 低层次)的类是TouchPoint,与XNATouchPanel很相似,不包括手势对于手势的识别和判断。对于手势的识别和判断是在high-level( 高层次)接口中实现,定义在 UIElement class的三个事件中: ManipulationStarted, ManipulationDelta, and ManipulationCompleted
 
201103031529498962.png
 
 
 
Represents a single touch point from a multitouch message source.
Name
Description
Gets the type of action (up, down, move) that the TouchPoint represents.
Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Gets the X,Y coordinate position of the touch point as a Point.
Gets the rectangular area that is reported as the touch-point contact area.
Gets the specific device type that produced the touch point.
 
 
 
要使用低级别的触摸界面,需要在代码中增加的静态Touch.FrameReported事件处理程序: 
 
Touch.FrameReported += OnTouchFrameReported;
OnTouchFrameReported 方法的用法如下:
void OnTouchFrameReported(object sender, TouchFrameEventArgs args) {
… }
201103031529503846.png
 
Name
Description
Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Serves as a hash function for a particular type. (Inherited from Object.)
Returns the primary touch point for the reported frame as a TouchPoint value. The touch point can be evaluated against a coordinate reference point instead of the absolute Silverlight coordinates.
Returns a collection of all TouchPoint values in the reported frame. The touch point can be evaluated against a coordinate reference point instead of the absolute Silverlight coordinates.
Gets the Type of the current instance. (Inherited from Object.)
Creates a shallow copy of the current Object. (Inherited from Object.)
Disables automatic mouse-event promotion for the primary touch point until all touch points report as Up.
Returns a String that represents the current Object. (Inherited from Object.)
GetPrimaryTouchPoint 返回主要接触点的对象,而GetTouchPoints返回的接触点的合集。
如果不希望被提升为鼠标事件,事件处理程序通常是这样:
void OnTouchFrameReported(object sender, TouchFrameEventArgs args)
{
TouchPoint primaryTouchPoint = args.GetPrimaryTouchPoint(null);
if (primaryTouchPoint != null && primaryTouchPoint.Action == TouchAction.Down)
{
args.SuspendMousePromotionUntilTouchUp();
}
… }

Silverlight的多点触控

在游戏程序和乐器程序中有时需要用到多点触控操作,Windows Phone 7支持四点同时触控。当触控的边界以3.5毫米或者更大的单位分隔时,每个大于7毫米以上的屏幕触控,都被视为单一的触控指令,并支持对于手势的识别。每个触控点都会增加处理器的负载,因此,程序开发者启用多个两个同时触控点的功能是,要注意是否会影响性能。
Silverlight通过三个事件的响应实现多点触控,分别是: ManipulationStarted, ManipulationDelta, and ManipulationCompleted. 当Touch.FrameReported在整个应用程序中传递触控消息时,在本例 SilverlightTapHello1ManipulationStarted事件将处理后的结果显示在 TextBlock上。
Silverlight Project: SilverlightTapHello1 File: MainPage.xaml (excerpt)
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Text="Hello, Windows Phone 7!"
Padding="0 34"
HorizontalAlignment="Center" VerticalAlignment="Center" ManipulationStarted="OnTextBlockManipulationStarted" />
</Grid>
MainPage.xaml.cs包含此事件处理程序:
Silverlight Project: SilverlightTapHello1 File: MainPage.xaml.cs (excerpt)
public partial class MainPage : PhoneApplicationPage
{
Random rand = new Random();
 
public MainPage()
{
InitializeComponent();
}
void OnTextBlockManipulationStarted(object sender,
ManipulationStartedEventArgs args)
{
TextBlock txtblk = sender as TextBlock;
 
Color clr = Color.FromArgb(255, (byte)rand.Next(256),
(byte)rand.Next(256),
(byte)rand.Next(256));
 
txtblk.Foreground = new SolidColorBrush(clr);
 
args.Complete();
}
}
 
在模拟器上调试该程序时,会发现只有当点击文本框之内时才会发生文字的颜色改变,点击文本框之外字体的颜色并没有变成白色。这是因为本例中 ManipulationStarted事件是只针对单个独立的元素 TextBlock,所以做以下改进,在 MainPage中重载 OnManipulationStarted事件,通过判断ManipulationStartedEventArgs.OriginalSource来判断点击的是文本框之外还是文本框之内。代码如下:
 
 
public partial class MainPage : PhoneApplicationPage
{
Random rand = new Random();
Brush originalBrush;
 
public MainPage()
{
InitializeComponent();
originalBrush = txtblk.Foreground;
}
 
protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
{
if (args.OriginalSource == txtblk)
{
txtblk.Foreground = new SolidColorBrush(
Color.FromArgb(255, (byte)rand.Next(256),
(byte)rand.Next(256),
(byte)rand.Next(256)));
}
else
{
txtblk.Foreground = originalBrush;
}
 
args.Complete();
base.OnManipulationStarted(args);
}
}
 

Windows Phone 7支持的触控指令

 
单击Tap
201103031529505765.jpg
双击Double Tap
201103031529514652.jpg
拖曳(Pan
201103031529528914.png
滑动Flick
201103031529536863.png
点击并固定Touch and Hold
20110303152954975.png
缩小和延伸Pinch and Stretch
201103031529547287.png
 
 
 

Gesture Support for Windows Phone

Touch gestures are the primary method for a user to interact with a Windows Phone, requiring a user-initiated movement with single or multiple fingers on a touch screen. Tapping a UI element such as a push button is an example. Developers can implement gesture support in their application using either the Silverlight or XNA frameworks. For more information about how you can use the Silverlight or XNA frameworks to develop Windows Phone applications, see The Silverlight and XNA Frameworks for Windows Phone. Each framework offers unique ways to handle touch input to create compelling, interactive end user applications. This topic will summarize both the Silverlight and XNA gesture systems and provide links to topics and articles offering implementation guidance.
201103031529553351.gif Gesture Support in the Silverlight Framework
201103031529555826.png
Silverlight for Windows Phone allows you to process touch input by using manipulation events. By using these events, you can move and scale objects in response to touch and multi-touch input. The events are described in the following table.
Event
Description
ManipulationStarted
This event occurs when the user starts a direct manipulation by placing their finger or fingers on the screen.
ManipulationDelta
This event occurs repeatedly while the user is moving their finger or fingers on the screen.
ManipulationCompleted
This event occurs when the user removed their finger or fingers from the screen
For implementation guidance about how to handle these manipulation events, see How to: Handle Manipulation Events. This topic will provide instruction for creating a simple application that responds to single and multi-touch input.
201103031529556665.gif Note:
Silverlight controls that are supported on Windows Phone are gesture-aware, and support gestures such as tap, pan, and flick. You can handle simple gestures such as tap, double-tap, and tap-and-hold by using mouse events. For more information, see the Input for Windows Phone topic.
201103031529553600.gif Gesture Support in the XNA Framework
201103031529563252.png
The XNA framework offers a robust touch gesture system for developing applications. Developers can leverage this built-in gesture system instead of implementing one from scratch. The following XNA gestures are supported in Windows Phone.
GestureType
Description
Tap
手指单击
DoubleTap
This gesture represents two taps in succession.
Hold
A finger touches the screen and holds it in place for a brief period of time.
FreeDrag
A finger touches the screen and moves in any direction.
VerticalDrag
A finger touches the screen and moves in an up or down direction.
HorizontalDrag
A finger touches the screen and moves in a left or right direction.
DragComplete
Marks the end of a FreeDrag, VerticalDrag, or HorizontalDrag gesture.
Flick
A finger drags across the screen and is lifted up without stopping.
Pinch
Two fingers press on the screen and move around.
PinchComplete
This gesture marks the end of a Pinch gesture.
The following list provides useful topics and articles for implementing gesture support in an XNA application:
 

转载于:https://www.cnblogs.com/xuesong/archive/2011/03/03/1969970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值