Windows Phone dev
我刚刚发布了一个App.(具体可以看看这里)。这个应用可以让用户使用几个手指同时在屏幕上触摸并作出反应。好了,我们一起来看看这是如何做到的。
因为这个演示是要用到 TouchFrameEventhandler 事件的,所以你必须添加以下.net命名空间。
System.Windows.Input.Touch
然后建立一个类,叫做 DrawingPoints,接着创建一个包含大量此对象的列表集合,如下图代码所示。
class DrawingPoints
{
public int id { get; set; }
public Point currentPoint { get; set; }
public Point oldPoint { get; set; }
public bool drawing { get; set; }
public SolidColorBrush currentColor { get; set; }
public double drawSize { get; set; }
}
List drawingPoints = new List();

接着在TouchEvent事件触发的处理方法中添加以下代码
void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
//This holds a collection of touches on the screen. So for each finger that touches the canvas one touch get added.
TouchPointCollection tCollection = e.GetTouchPoints(DrawingCanvas);
if (tCollection != null)
{
//iterate through the collection of touches
foreach (var point in tCollection)
{
//sets up a drawing point
DrawingPoints drawPoint;
//if the touch action is the finger leaving the screen, remove the drawing point from the list of drawpoints
if (point.Action == TouchAction.Up)
{
var oldPoint = drawingPoints.Single(x => x.id == point.TouchDevice.Id);
if (oldPoint != null)
{
drawingPoints.Remove(oldPoint);
}
}
//if the touch action is down, add the point to the drawingpoint
if (point.Action == TouchAction.Down)
{
//gets a list of colors to set which color is set as a drawing point
var colors = App.GetDrawingColors();
var color = colors.Single(c => c.ColorID == settings.DrawColor);
var size = settings.DrawSize;
//create a drawpoint and assing it Touchevent details
drawPoint = new DrawingPoints();
drawPoint.id = point.TouchDevice.Id; //sets the drawpoint an ID so it knows which finger is touching the screen
drawPoint.currentPoint = point.Position; //gets the position
drawPoint.oldPoint = drawPoint.currentPoint;
drawPoint.currentColor = color.ColorBrush;
drawPoint.drawSize = size;
drawingPoints.Add(drawPoint);
}
//if the touch action is move, move the point and draw a path from the old point to the new point
if (point.Action == TouchAction.Move)
{
drawPoint = drawingPoints.Single(x => x.id == point.TouchDevice.Id);
drawPoint.currentPoint = point.Position;
draw(drawPoint); //calls the draw action method
drawPoint.oldPoint = drawPoint.currentPoint;
}
}
}
}

这样每当手指触碰到屏幕或者屏幕的某个可见元素时,TouchCollection在代码中就添加一个TouchPoint。
然后对于TouchCollection里的每一个TouchPoint,都去检测这个触摸点对应的行为,但其实说白了,这些行为总共就3种,Up — 手指离开屏幕,Down — 手指触摸到屏幕以及 Move — 手指在屏幕上移动。
从上面的代码看到:当触摸行为是Up时,drawingPoints集合去除这个点然后停止绘制。当行为为Down时,drawingPoints集合添加这个点并做好移动行为的准备,所以当你手指移动的时候,就会在手指当前位置与之前的点坐标绘制出一条直线。
好了,现在你将要在程序进入到此页面后立即订阅这个多点触摸事件处理方法。以及程序退出该页面时取消订阅这个事件的处理方法。
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported); //Adds the event to the page
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
Touch.FrameReported -= new TouchFrameEventHandler(Touch_FrameReported); //removes the event from the page
}

综上所述,这就是我刚发布的那个应用的精髓所在。如果你有更多的问题请email我吧(不过我只会英语哦:D)。