Windows Phone笔记(3)触摸简介
Windows Phone手機的屏幕非常靈敏,至少能夠同時檢測4個手指的多點觸摸,這種多個手指間的互動使得開發者在處理多點觸摸時面臨了很大的挑戰,在Silverlight框架中,觸摸輸入是通過事件來獲取的。
Silverlight支持兩種不同的編程接口來支持多點觸摸,分別為:底層接口和高層接口。其中,
底層接口:是基于靜態的Touch.FrameReported事件;
高層接口:是由UIElement類中定義3個事件組成,這些事件統稱為:Manipulation事件。
?
1.底層接口
Silverlight底層觸摸接口的核心是:TouchPoint類型,TouchPoint的每個實例分別表示觸摸屏幕的一個特定手勢。
TouchPoint具有四個只讀屬性:
| 屬性 | 類型 | 說明 |
| Action | TouchAction | 枚舉類型,包含Down、Move、Up三個成員 |
| Position | Point | 表示觸摸坐標,相對于特定元素左上角的位置,稱為參考(reference)元素。 |
| Size | Size | 類型為Size,表示接觸面積(以及手指壓力大小),但在Windows Phone中目前還沒有作用。 |
| TouchDevice | TouchDevice | 有兩個只讀屬性:①.Id,類型為int,用于區分不同的手勢,從Down到Up的所有事件中,一個特定的手勢是由一個唯一的Id。 ? ?②.DirectlyOver,類型為UIElement,手指下的最頂層的元素,通常用來確定用戶正在觸摸的元素。 |
?
下面讓我們來觀察一個示例,該示例的效果是:只要觸摸屏幕內的任何元素,TextBlock元素的文本顏色就會隨機改變:
首先在MainPage.xaml中增加一個TextBlock元素:
1 <TextBlock Name="txtblk" Text="Hello,Windows Phone!" Padding="0 34" HorizontalAlignment="Center" VerticalAlignment="Center"/>?
要使用底層觸摸接口需要為靜態的Touch.FrameReported事件注冊一個事件處理程序。
1 Touch.FrameReported += OnTouchFrameReported;//綁定事件?
下面是OnTouchFrameReported方法的代碼,該事件處理程序獲取應用程序中所有的觸摸事件(其中,"rand"變量是Random類的一個實例,隨機生成RGB顏色)。
1 private void OnTouchFrameReported(Object sender, TouchFrameEventArgs args)2 {
3 TouchPoint primaryTouchPoint = args.GetPrimaryTouchPoint(null);//獲取觸屏點左上角的位置
4
5 if (primaryTouchPoint != null && primaryTouchPoint.Action == TouchAction.Down)//判斷觸控的類型
6 {
7 this.txtblk.Foreground = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256)));//隨機改變TextBlock文本的顏色
8 }
9 }
?
請注意這個方法是效果:即使我們點擊TextBlock以外的區域,也同樣會觸發OnTouchFrameReported事件,這顯然沒有達到我們的預期,但是如何把事件的作用只限制在TextBlock控件中呢?很簡單,只需要加一個If判斷就可以了:
1 private void OnTouchFrameReported(Object sender, TouchFrameEventArgs args)2 {
3 TouchPoint primaryTouchPoint = args.GetPrimaryTouchPoint(null);
4
5 if (primaryTouchPoint != null && primaryTouchPoint.Action == TouchAction.Down)//判斷觸控的類型
6 {
7 if (primaryTouchPoint.TouchDevice.DirectlyOver == this.txtblk)//獲取觸控的最頂層元素
8 {
9 this.txtblk.Foreground = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256)));
10 }
11 }
12 }
這樣,就除了觸摸TextBlock元素以外都不會觸發該事件了。運行如下:
?
2.高層接口
Silverlight中的高層接口包含了3個事件:①ManipulationStarted ?②ManipulationDelta??③ManipulationCompleted。這些事件并不處理單個的手勢活動,而是將多個手勢的活動合并到轉換和縮放中。這些事件也累加了速度信息,可以用來實現慣性操作。
高層接口和底層接口的區別在于:
Touch.FrameReported為整個應用程序傳遞觸摸信息,而Manipulation事件是基于單個元素的。
?下面讓我們來查看一個示例程序:
這是MainPage.xaml代碼,我們直接在xaml中注冊事件:
1 <TextBlock Text="Hello Windows Phone" Padding="0 34" HorizontalAlignment="Center" VerticalAlignment="Center" ManipulationStarted="TextBlock_ManipulationStarted"/>2 </Grid>
?
MainPage.xmal.cs中的事件處理程序:
1 public partial class MainPage : PhoneApplicationPage2 {
3 // 構造函數
4 Random rand = new Random();
5 public MainPage()
6 {
7 InitializeComponent();
8 }
9
10 private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
11 {
12 TextBlock txtblk = sender as TextBlock;
13 Color clr = Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256));
14
15 txtblk.Foreground = new SolidColorBrush(clr);
16 e.Complete();//不是必須的,表示已經完成了操作
17 }
18 }
在這個事件處理程序中,只有觸摸了TextBlock控件,才會觸發這個事件。
?
換一種方式能夠實現同樣類似的效果,而且更簡單 —— UIElement類定義了所有的Manipulation事件,而Control(MainPage的基類)為這些事件提供了protected的虛方法,現在并不需要為MainPage頁面注冊Manipulation事件的處理程序,只需要重寫這個虛方法就可以了。
參考下面的示例:
MainPage.xmal
<TextBlock Name="txtblk" Padding="0 34" VerticalAlignment="Center" HorizontalAlignment="Center" Text="Hello,Windows Phone"/>?
MainPage.xmal.cs
1 public partial class MainPage : PhoneApplicationPage2 {
3 Random rand = new Random();
4 Brush originalBrush;
5 // 構造函數
6 public MainPage()
7 {
8 InitializeComponent();
9 originalBrush = this.txtblk.Foreground;
10 }
11
12 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
13 {
14 if (e.OriginalSource == this.txtblk)
15 {
16 this.txtblk.Foreground = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256)));
17 }
18 else
19 {
20 this.txtblk.Foreground = originalBrush;
21 }
22
23 e.Complete();
24 base.OnManipulationStarted(e);
25 }
26 }
在上面的示例中我們可以通過OriginalSource屬性知道事件在可視化樹中的真實來源,也就是實際觸發事件的元素。這得益于Sliverlight的一個功能特征路由事件處理(routed event handling).
?
3.路由事件
如果頂層元素不關心一個事件的話,該事件就會轉發到該元素的父元素去,這樣,一直轉發到可視化樹最高級的PhoneApplicationFrame元素為止,沿途的任何元素都可以獲取這個輸入事件并進行處理,也能阻止事件往樹的高層繼續傳遞。
那么根據上面的原理我們編寫一個新的示例,注冊一個Manipulation事件和重寫Manipulation事件的虛方法,前者只處理TextBlock的觸摸事件,后者負責處理MainPage頁面上的所有觸摸事件。
MainPage.xmal:
<TextBlock Name="txtblk" Text="Hello,Windows Phone" Padding="0 34" FontSize="28" VerticalAlignment="Center" HorizontalAlignment="Center" ManipulationStarted="txtblk_ManipulationStarted"/>?
MainPage.xmal.cs:
1 public partial class MainPage : PhoneApplicationPage2 {
3 Random rand = new Random();
4 Brush originalBrush;
5 // 構造函數
6 public MainPage()
7 {
8 InitializeComponent();
9 originalBrush = this.txtblk.Foreground;
10 }
11
12 private void txtblk_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
13 {
14 this.txtblk.Foreground = new SolidColorBrush(Color.FromArgb(255,(byte)rand.Next(256),(byte)rand.Next(256),(byte)rand.Next(256)));
15 e.Complete();
16 e.Handled = true;//表示事件已經處理,不需要再進一步傳遞到可視化樹的上層了
17 }
18
19 protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
20 {
21 this.txtblk.Foreground = originalBrush;
22
23 e.Complete();
24 base.OnManipulationStarted(e);
25 }
26 }
程序運行效果:
觸摸TextBlock隨機改變文本顏色 ?觸摸TextBlock以外的區域TextBlock文本顏色將回復到初始狀態
? ?
?
參考資料:
http://msdn.microsoft.com/zh-cn/library/cc189018(v=vs.95).aspx
《Programming Windows Phone 7 Microsoft Silverlight Edition》
作者:晴天豬
出處:http://www.cnblogs.com/IPrograming
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
?Windows Phone開發者交流群:79339880,歡迎大家來一起討論交流,共同學習進步。
本文轉自gyzhao博客園博客,原文鏈接:http://www.cnblogs.com/IPrograming/archive/2012/02/17/WindowsPhone_Touch_About.html,如需轉載請自行聯系原作者總結
以上是生活随笔為你收集整理的Windows Phone笔记(3)触摸简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #44
- 下一篇: 虚拟机与系统文件互传(VMware To