天天看点

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对象有两个得到只读属性: 

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

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

代码  

<phone:PhoneApplicationPage   

    x:Class="SilverlightTouchHello.MainPage" 

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

    FontFamily="{StaticResource PhoneFontFamilyNormal}" 

    FontSize="{StaticResource PhoneFontSizeNormal}" 

    Foreground="{StaticResource PhoneForegroundBrush}" 

    SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" 

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 

    shell:SystemTray.IsVisible="True"> 

    <!--LayoutRoot contains the root grid where all other page content is placed--> 

    <Grid x:Name="LayoutRoot" Background="Transparent"> 

        <Grid.RowDefinitions> 

            <RowDefinition Height="Auto"/> 

            <RowDefinition Height="*"/> 

        </Grid.RowDefinitions> 

        <!--TitlePanel contains the name of the application and page title--> 

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 

            <TextBlock x:Name="ApplicationTitle" Text="SILVERLIGHT TOUCH HELLO" Style="{StaticResource PhoneTextNormalStyle}"/> 

            <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 

        </StackPanel> 

        <!--ContentPanel - place additional content here--> 

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

    </Grid> 

    <!-- Sample code showing usage of ApplicationBar  

    <phone:PhoneApplicationPage.ApplicationBar> 

        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 

            <shell:ApplicationBarIconButton x:Name="appbar_button1" IconUri="/Images/appbar_button1.png" Text="Button 1"></shell:ApplicationBarIconButton> 

            <shell:ApplicationBarIconButton x:Name="appbar_button2" IconUri="/Images/appbar_button2.png" Text="Button 2"></shell:ApplicationBarIconButton> 

            <shell:ApplicationBar.MenuItems> 

                <shell:ApplicationBarMenuItem x:Name="menuItem1" Text="MenuItem 1"></shell:ApplicationBarMenuItem> 

                <shell:ApplicationBarMenuItem x:Name="menuItem2" Text="MenuItem 2"></shell:ApplicationBarMenuItem> 

            </shell:ApplicationBar.MenuItems> 

        </shell:ApplicationBar> 

    </phone:PhoneApplicationPage.ApplicationBar> 

    --> 

</phone:PhoneApplicationPage> 

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;  

            }  

    }  

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