且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-03 10:21:00

在WP7上Silverlight还支持多点触摸,有两种不同的编程模式:

1、低级别使用Touch.FrameReported事件

2、高级别的使用UIElement类中定义三个事件:ManipulationStarted,ManipulationDelta和ManipulationCompleted。

一、

第一种低级别的触摸编程是使用类TouchPoint,一个TouchPoint的实例代表一个特定的手指触摸屏幕。

TouchPoint的四个属性: 
• 动作的类型-枚举TouchAction,有Down, Move和Up四个值表示手指的按下、移动和离开。 
• 位置的类型-Point的位置,以左上角为参考点。 
• 大小的类型-Size,支持接触面积(手指的压力大小),但Windows 7不会返回电话有用的值。 
• 接触设备的类型TouchDevice。

该TouchDevice对象有两个得到只读属性: 
•ID int类型,用来区分手指,一个特定的手指有一个唯一测ID来触发所有的上下移动的事件。

• DirectlyOver UIElement的类型,你手指的最顶层元素。

 

使用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属性相对于应用程序的左上角。

实例单点触摸改变字体的颜色

 

 

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


  1. 代码  
  2.  
  3. <phone:PhoneApplicationPage   
  4.     x:Class="SilverlightTouchHello.MainPage" 
  5.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  6.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  7.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  8.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  9.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  10.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  12.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  13.     Foreground="{StaticResource PhoneForegroundBrush}" 
  14.     SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" 
  15.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  16.     shell:SystemTray.IsVisible="True"> 
  17.  
  18.     <!--LayoutRoot contains the root grid where all other page content is placed--> 
  19.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  20.         <Grid.RowDefinitions> 
  21.             <RowDefinition Height="Auto"/> 
  22.             <RowDefinition Height="*"/> 
  23.         </Grid.RowDefinitions> 
  24.  
  25.         <!--TitlePanel contains the name of the application and page title--> 
  26.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  27.             <TextBlock x:Name="ApplicationTitle" Text="SILVERLIGHT TOUCH HELLO" Style="{StaticResource PhoneTextNormalStyle}"/> 
  28.             <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  29.         </StackPanel> 
  30.  
  31.         <!--ContentPanel - place additional content here--> 
  32.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  33.             <TextBlock Name="txtblk" 
  34.                        Text="Hello, Windows Phone 7!" 
  35.                        Padding="0 22" 
  36.                        HorizontalAlignment="Center" 
  37.                        VerticalAlignment="Center" /> 
  38.         </Grid> 
  39.     </Grid> 
  40.       
  41.     <!-- Sample code showing usage of ApplicationBar  
  42.     <phone:PhoneApplicationPage.ApplicationBar> 
  43.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
  44.             <shell:ApplicationBarIconButton x:Name="appbar_button1" IconUri="/Images/appbar_button1.png" Text="Button 1"></shell:ApplicationBarIconButton> 
  45.             <shell:ApplicationBarIconButton x:Name="appbar_button2" IconUri="/Images/appbar_button2.png" Text="Button 2"></shell:ApplicationBarIconButton> 
  46.             <shell:ApplicationBar.MenuItems> 
  47.                 <shell:ApplicationBarMenuItem x:Name="menuItem1" Text="MenuItem 1"></shell:ApplicationBarMenuItem> 
  48.                 <shell:ApplicationBarMenuItem x:Name="menuItem2" Text="MenuItem 2"></shell:ApplicationBarMenuItem> 
  49.             </shell:ApplicationBar.MenuItems> 
  50.         </shell:ApplicationBar> 
  51.     </phone:PhoneApplicationPage.ApplicationBar> 
  52.     --> 
  53.  
  54.  
  55. </phone:PhoneApplicationPage> 

 

 


  1. 代码  
  2.  
  3. using System;  
  4. using System.Windows.Input;  
  5. using System.Windows.Media;  
  6. using Microsoft.Phone.Controls;  
  7.  
  8. namespace SilverlightTouchHello  
  9. {  
  10.     public partial class MainPage : PhoneApplicationPage  
  11.     {  
  12.         Random rand = new Random();  
  13.         Brush originalBrush;  
  14.           
  15.         public MainPage()  
  16.         {  
  17.             InitializeComponent();  
  18.             originalBrush = txtblk.Foreground;  
  19.             Touch.FrameReported += OnTouchFrameReported;  
  20.         }  
  21.  
  22.         void OnTouchFrameReported(object sender, TouchFrameEventArgs args)  
  23.         {  
  24.             TouchPoint primaryTouchPoint = args.GetPrimaryTouchPoint(null);  
  25.  
  26.             if (primaryTouchPoint != null && primaryTouchPoint.Action == TouchAction.Down)  
  27.             {  
  28.                 if (primaryTouchPoint.TouchDevice.DirectlyOver == txtblk)  
  29.                 {  
  30.                     txtblk.Foreground = new SolidColorBrush(  
  31.                                 Color.FromArgb(255, (byte)rand.Next(256),  
  32.                                                     (byte)rand.Next(256),  
  33.                                                     (byte)rand.Next(256)));  
  34.                 }  
  35.                 else  
  36.                 {  
  37.                     txtblk.Foreground = originalBrush;  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079241