



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Windows Phone 7 多点触摸编程 一、基本的程序结构一个需要响应多点触控的 Silverlight 应用程序必须将一个处理程序连接到静态 Touch.FrameReported 事件:Touch.FrameReported += OnTouchFrameReported;FrameReported 事件是静态 Touch 类的唯一公共成员。处理程序如下所示:void OnTouchFrameReported(object sender, TouchFrameEventArgs args) .您可以在应用程序中安装多个 Touch.FrameReported 事件处理程序,所有这些事件处理程序都会报告应用程序中任何位置的所有触控事件。二、事件传入参数TouchFrameEventArgsTouchFrameEventArgs 有一个名为 TimeStamp 的公共属性(我还没有机会使用)和三个重要的公共方法:TouchPoint GetPrimaryTouchPoint(UIElement relativeTo)TouchPointCollection GetTouchPoints(UIElement relativeTo)void SuspendMousePromotionUntilTouchUp()GetPrimaryTouchPoint 或 GetTouchPoints 的参数仅用于报告 TouchPoint 对象的位置信息。您可以将空值用于此参数,位置信息相对于整个 Silverlight 应用程序的左上角。多点触控支持多个手指同时触摸屏幕,触摸屏幕的每个手指(数量存在上限,当前通常为两个)都是一个触摸点。主要触摸点是指在没有其他手指触摸屏幕并且未按下鼠标按钮时触摸屏幕的手指。用一个手指触摸屏幕。这是主要触摸点。在第一个手指仍触摸着屏幕时,将第二个手指放在屏幕上。很显然,第二手指不是主要触摸点。但现在仍将第二个手指放在屏幕上,抬起第一个手指,然后再将其放回到屏幕上。这是主要触摸点吗?不,都不是。仅当没有其他手指触摸屏幕时,才会出现主要触摸点。在实际的多点触控应用程序中,您应该注意不要依赖主要触摸点,因为用户通常不会重视第一次触摸的特定意义。仅为实际触摸屏幕的手指激发事件。对非常接近屏幕但并未触摸屏幕的手指,不会进行悬停检测。基于一个触摸点或多个触摸点激发一个特殊的 Touch.FrameReported 事件。从 GetTouchPoints 方法返回的 TouchPointCollection 包含与特定事件关联的所有触摸点。从 GetPrimaryTouchPoint 返回的 TouchPoint 始终是一个主要触摸点。如果没有与该特定事件关联的主要触摸点,GetPrimaryTouchPoint 将返回 null。即使从 GetPrimaryTouchPoint 返回的 TouchPoint 不是 null,它也不会与从 GetTouchPoints 返回的其中一个 TouchPoint 对象是相同对象,但在传递给这些方法的参数相同时所有属性将相同。TouchPoint 类定义以下四个只读属性,这些属性都由依赖属性支持:Action 属性,类型为 TouchAction(一个具有 Down、Move 和 Up 成员的枚举)。Position 属性,类型为 Point,它相对于作为参数传递给 GetPrimaryTouchPoint 或 GetTouchPoints 方法的元素(或相对于参数为 null 的应用程序的左上角)。Size 属性,类型为 Size。TouchDevice 属性,类型为 TouchDevice。TouchDevice 对象具有两个也由依赖属性支持的只读属性:DirectlyOver 属性,类型为 DirectlyOver(手指下最上面的元素)。Id 属性,类型为 int。DirectlyOver 不必是传递给 GetPrimaryTouchPoint 或 GetTouchPoints 的元素的子项。如果手指在 Silverlight 应用程序内(由 Silverlight 插件对象的尺寸定义),但不在可点击测试控件覆盖的范围内,则此属性可以为空。(具有空背景刷的面板不可点击测试。)若要在多个手指之间区分,ID 属性至关重要。在特定手指触摸屏幕时,与该手指关联的一系列特定事件总是以 Down 操作开始,接着是 Move 事件,最后是 Up 事件。所有这些事件都将与相同的 ID 关联。(但不要以为主要触摸点的 ID 值将为 0 或 1。)大多数重要的多点触控代码都会使用 Dictionary 集合,其中 TouchDevice 的 ID 属性是字典键。这是跨事件存储特定手指信息的方式。三、涂鸦板例子 涂鸦板例子 综合运用ApplicationBar菜单、多点触摸和IsolatedStorageFile本地存储xamlView Code csView Code using System;using System.Collections.Generic;using System.Windows;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using Microsoft.Phone.Controls;using Microsoft.Phone.Shell;namespace Jot public partial class MainPage : PhoneApplicationPage JotAppSettings appSettings = (Application.Current as App).AppSettings; Dictionary activeStrokes = new Dictionary(); public MainPage() InitializeComponent(); inkPresenter.Strokes = appSettings.StrokeCollectionsappSettings.PageNumber; inkPresenter.Background = new SolidColorBrush(appSettings.Background); / Re-assign ApplicationBar button names appbarLastButton = this.ApplicationBar.Buttons1 as ApplicationBarIconButton; appbarNextButton = this.ApplicationBar.Buttons2 as ApplicationBarIconButton; appbarDeleteButton = this.ApplicationBar.Buttons3 as ApplicationBarIconButton; TitleAndAppbarUpdate(); Touch.FrameReported += OnTouchFrameReported; /一个需要响应多点触控的 Silverlight 应用程序必须将一个处理程序连接到静态 Touch.FrameReported 事件 /处理多点触摸事件 void OnTouchFrameReported(object sender, TouchFrameEventArgs args) TouchPoint primaryTouchPoint = args.GetPrimaryTouchPoint(null); if (primaryTouchPoint != null & primaryTouchPoint.Action = TouchAction.Down) args.SuspendMousePromotionUntilTouchUp(); /* * foreach 循环枚举从 GetTouchPoints 返回的 TouchPointCollection 的 TouchPoint 成员。 * (您会希望多点触控感知 Silverlight 程序能处理多个手指,但您不会希望它在检测到太多手指时出现故障!)ID 将添加到 Down 事件的字典中,然后从 Up 事件的字典中删除。 * */ TouchPointCollection touchPoints = args.GetTouchPoints(inkPresenter); foreach (TouchPoint touchPoint in touchPoints) Point pt = touchPoint.Position; int id = touchPoint.TouchDevice.Id; switch (touchPoint.Action) case TouchAction.Down: Stroke stroke = new Stroke(); stroke.DrawingAttributes.Color = appSettings.Foreground; stroke.DrawingAttributes.Height = appSettings.StrokeWidth; stroke.DrawingAttributes.Width = appSettings.StrokeWidth; stroke.StylusPoints.Add(new StylusPoint(pt.X, pt.Y); inkPresenter.Strokes.Add(stroke); activeStrokes.Add(id, stroke); break; case TouchAction.Move: activeStrokesid.StylusPoints.Add(new StylusPoint(pt.X, pt.Y); break; case TouchAction.Up: activeStrokesid.StylusPoints.Add(new StylusPoint(pt.X, pt.Y); activeStrokes.Remove(id); TitleAndAppbarUpdate(); break; /新增一个页面 void OnAppbarAddClick(object sender, EventArgs args) StrokeCollection strokes = new StrokeCollection(); appSettings.PageNumber += 1; appSettings.StrokeCollections.Insert(appSettings.PageNumber, strokes); inkPresenter.Strokes = strokes; TitleAndAppbarUpdate(); /上一个页面 void OnAppbarLastClick(object sender, EventArgs args) appSettings.PageNumber -= 1; inkPresenter.Strokes = appSettings.StrokeCollectionsappSettings.PageNumber; TitleAndAppbarUpdate(); /下一个页面 void OnAppbarNextClick(object sender, EventArgs args) appSettings.PageNumber += 1; inkPresenter.Strokes = appSettings.StrokeCollectionsappSettings.PageNumber; TitleAndAppbarUpdate(); /删除当前的页面 void OnAppbarDeleteClick(object sender, EventArgs args) MessageBoxResult result = MessageBox.Show(Delete this page?, Jot, MessageBoxButton.OKCancel); if (result = MessageBoxResult.OK) if (appSettings.StrokeCollections.Count = 1) appSettings.StrokeCollections0.Clear(); else appSettings.StrokeCollections.RemoveAt(appSettings.PageNumber); if (appSettings.PageNumber = appSettings.StrokeCollections.Count) appSettings.PageNumber -= 1; inkPresenter.Strokes = appSettings.StrokeCollectionsappSettings.PageNumber; TitleAndAppbarUpdate(); /设置画笔背景颜色 void OnAppbarSwapColorsClick(object sender, EventArgs args) Color foreground = appSettings.Background; appSettings.Background = appSettings.Foreground; appSettings.Foreground = foreground; inkPresenter.Background = new SolidColorBrush(appSettings.Background); foreach (StrokeCollection strokeCollection in appSettings.StrokeCollections) foreach (Stroke stroke in strokeCollection) stroke.DrawingAttributes.Color = appSettings.Foreground; /设置画笔的粗细 void OnAppbarSetStrokeWidthClick(object sender, EventArgs args) ApplicationBarMenuItem item = sender as ApplicationBarMenuItem; if (item.Text.StartsWith(light) appSettings.StrokeWidth = 1; else if (item.Text.StartsWith(medium) appSettings.StrokeWidth = 3; else if (item.Text.StartsWith(heavy) appSettings.StrokeWidth = 5; void TitleAndAppbarUpdate() pageInfoTitle.Text = String.Format( - PAGE 0 OF 1, appSettings.PageNumber + 1, appSettings.StrokeCollections.Count); appbarLastButton.IsEnabled = appSettings.PageNumber 0; appbarNextButton.IsEnabled = appSettings.PageNumber 1) | (appSettings.StrokeCollections0.Count 0); 本地存储功能封装的类View Code using System;using System.Collections.Generic;using System.IO;using System.IO.IsolatedStorage;using System.Windows;using System.Windows.Ink;using System.Windows.Media;using System.Xml.Serialization;namespace Jot public class JotAppSettings public JotAppSettings() this.PageNumber = 0; this.Foreground = (Color)Application.Current.ResourcesPhoneForegroundColor; this.Background = (Color)Application.Current.ResourcesPhoneBackgroundColor; this.StrokeWidth = 3; / Public properties - the actual application settins public List StrokeCollections get; set; public int PageNumber set; get; public Color Foreground set; get; public Color Background set; get; public int StrokeWidth set; get; public static JotAppSettings Load() JotAppSettings settings; IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); if (iso.FileExists(settings.xml) IsolatedStorageFileStream stream = iso.OpenFile(settings.xml, FileMode.Open); StreamReader reader = new StreamReader(stream); XmlSerializer ser = new XmlSerializer(typeof(JotAppSettings); settings = ser.Deserialize(reader) as JotAppSettings; reader.Close(); else / Create and initialize new JotAppSettings object settings = new JotAppSettings(); settings.StrokeCollections = new List(); settings.StrokeCollections.Add(new StrokeCollection(); iso.Dispose(); return settings; public void Save() IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream stream = iso.CreateFile(settings.xml); StreamWriter writer = new StreamWriter(stream); XmlSerializer ser = new XmlSerializer(typeof(JotAppSettings); ser.Serialize(writer, this); writer.Close(); iso.Dispose(); app.xamlView Code app.xaml.csView Code using System;using System.Windows;using System.Windows.Navigation;using Microsoft.Phone.Controls;using Microsoft.Phone.Shell;namespace Jot public partial class App : Application / Application Settings public JotAppSettings AppSettings set; get; / Easy access to the root frame public PhoneApplicationFrame RootFrame get; private set; / Constructor public App() / Global handler for uncaught exceptions. / Note that exceptions thrown by ApplicationBarItem.Click will not get caught here. UnhandledException += Application_UnhandledException; / Standard Silverlight initialization InitializeComponent(); / Phone-specific initialization InitializePhoneApplication(); / Code to execute when the application is launching (eg, from Start) / This code will not execute when the application is reactivated private void Application_Launching(object sender, LaunchingEventArgs e) AppSettings = JotAppSettings.Load(); / Code to execute when the application is activated (brought to foreground) / This code will not execute when the application is first launched private void Application_Activated(object sender, ActivatedEventArgs e) AppSettings = JotAppSettings.Load(); / Code to execute when the application is deactivated (sent to background) / This code will not execute when the application is closing private void Application_Deactivated(object sender, DeactivatedEventArgs e) AppSettings.Save(); / Code to execute when the application is closing (eg, user hit Back) / This code will not execute when the application is deactivated private void Application_Closing(object sender, ClosingEventArgs e) AppSettings.Save(); / Code to
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- IT服务平台管理办法
- 质疑与投诉管理办法
- 从事演艺演员管理办法
- 西餐餐饮规范管理办法
- 中国邮政速递管理办法
- 装饰公司仓库管理办法
- 网格员激励管理办法
- 薪酬体系及管理办法
- 中国消费养老管理办法
- 药店业务员管理办法
- 物业收费员的培训
- 总医院医共体信息化建设项目公开文件招投标书范本
- 参考建筑工程各项应急救援演练记录
- 设计版权授权合同范例
- DB43T 2558-2023 城镇低效用地识别技术指南
- 山东省第五届财会知识大赛试题及答案
- 个人给公司的投资协议书范本
- 2024年安徽省地勘行业职业技能大赛(地质调查员)考试题库(含答案)
- 2024养老院房屋租赁合同
- 输血指南的循证医学更新
- 2024年第九届中小学“学宪法、讲宪法”活动知识素养竞赛题库
评论
0/150
提交评论