天天看點

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