kingbosster 发表于 2015-5-10 12:08:18

Windows Phone 7 触摸编程-单点触摸利用Touch.FrameReported事件

  在WP7上Silverlight还支持多点触摸,有两种不同的编程模式:
  1、低级别使用Touch.FrameReported事件
  2、高级别的使用UIElement类中定义三个事件:ManipulationStarted,ManipulationDelta和ManipulationCompleted。
  一、
  第一种低级别的触摸编程是使用类TouchPoint,一个TouchPoint的实例代表一个特定的手指触摸屏幕。
  TouchPoint的四个属性:
· 动作的类型-枚举TouchAction,有Down, Move和Up四个值表示手指的按下、移动和离开。
· 位置的类型-Point的位置,以左上角为参考点。
· 大小的类型-Size,支持接触面积(手指的压力大小),但Windows 7不会返回电话有用的值。
· 接触设备的类型TouchDevice。
  该TouchDevice对象有两个得到只读属性:
·IDint类型,用来区分手指,一个特定的手指有一个唯一测ID来触发所有的上下移动的事件。
  · DirectlyOverUIElement的类型,你手指的最顶层元素。
  
  使用Touch.FrameReported事件处理程序:
Touch.FrameReported + = OnTouchFrameReported;
  
OnTouchFrameReported 方法格式如下:
void OnTouchFrameReported(object sender, TouchFrameEventArgs args)
  {

  }
  TouchFrameEventArgs args事件有3个方法:
  · GetTouchPoints(refElement)返回一个TouchPointCollection   获取多个接触点的集合
· GetPrimaryTouchPoint(refElement)返回一个TouchPoint 获取第一个手指接触点
· SuspendMousePromotionUntilTouchUp()
  
返回值是相对于传递的参数元素接触点的相对值。
  当传递null的时候,GetTouchPoints得到Position属性相对于应用程序的左上角。
  实例单点触摸改变字体的颜色
  
  


代码




   
   
      
            
            
      
      
      
            
            
      
      
      
            
      
   
   




代码



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;
                }
            }
      }
    }
}

  
页: [1]
查看完整版本: Windows Phone 7 触摸编程-单点触摸利用Touch.FrameReported事件