热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

与众不同windowsphone(25)Input(输入)之捕获UIElement之外的触控操作,Silverlight方式捕获手势操作,XNA方式捕获手势操作,多点触控

原文:与众不同

原文:



与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触控

作者:



介绍
与众不同 windows phone 7.5 (sdk 7.1) 之输入

捕获 UIElement 之外的触控操作
Silverlight 方式捕获手势操作
XNA 方式捕获手势操作

示例
1、演示如何捕获 UIElement 之外的触控操作
OutsideCapture.xaml

phone:PhoneApplicationPage
x:
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="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:Design d:Design
shell:SystemTray.IsVisible="True"
Grid x:Name="LayoutRoot" Background="Transparent"
Rectangle Name="rect" Fill="Red"
MouseLeftButtOnDown="rect_MouseLeftButtonDown"
MouseLeftButtOnUp="rect_MouseLeftButtonUp"
MouseMove="rect_MouseMove" /
TextBlock Name="lblMsg" VerticalAlignment="Top" TextWrapping="Wrap" Text="用手指触摸红色方块,然后将手指移除红色方块区域" /
/Grid
/phone:PhoneApplicationPage

OutsideCapture.xaml.cs

/*
* 演示如何在 UIElement 外响应 UIElement 上的触控事件
*
* UIElement - UI 元素
* CaptureMouse() - 捕获外部触摸事件,这样即使触摸事件在 UIElement 之外也可以响应
* ReleaseMouseCapture() - 取消外部触摸事件的捕获
*
* 注:
* 调用 UIElement 的 CaptureMouse() 方法需要满足以下条件
* 1、当前没有任何 UIElement 正在捕获中
* 2、必须在 UIElement 的 MouseLeftButtonDown 事件中调用 CaptureMouse() 方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace Demo.Input.Touch
{
public partial class OutsideCapture : PhoneApplicationPage
{
public OutsideCapture()
{
InitializeComponent();
private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
rect.CaptureMouse();
lblMsg.Text = "触摸点坐标为:" + e.GetPosition(LayoutRoot).ToString();
private void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
rect.ReleaseMouseCapture();
private void rect_MouseMove(object sender, MouseEventArgs e)
{
// 调用了 rect.CaptureMouse() 之后,即使触控移出 rect 也可以响应触控事件
lblMsg.Text = "触摸点坐标为:" + e.GetPosition(LayoutRoot).ToString();
}
}
}

2、演示如何通过 Silverlight 捕获手势操作
ManipulationDemo.xaml

phone:PhoneApplicationPage
x:
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="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:Design d:Design
shell:SystemTray.IsVisible="True"
Grid x:Name="LayoutRoot" Background="Transparent"
StackPanel Orientation="Vertical"
TextBlock Text="请随便做一些手势操作,以查看演示效果" /
TextBlock Name="lblMsg" /
/StackPanel
/Grid
/phone:PhoneApplicationPage

ManipulationDemo.xaml.cs

/*
* 演示如何在 Silverlight 下捕获手势操作
*
* UIElement - UI 元素
* ManipulationStarted - 手势操作开始时触发的事件,即触摸屏幕时触发的事件(事件参数:ManipulationStartedEventArgs)
* ManipulationDelta - 手势操作过程中触发的事件(事件参数:ManipulationDeltaEventArgs)
* ManipulationCompleted - 手持操作完成时触发的事件(事件参数:ManipulationCompletedEventArgs)
*
*
* ManipulationStartedEventArgs
* ManipulationContainer - 手势操作的容器,即坐标的参照物
* ManipulationOrigin - 手势起始点坐标
* Complete() - 停止捕获手势操作,即不会再触发 ManipulationDelta 事件
*
* ManipulationDeltaEventArgs
* ManipulationContainer - 手势操作的容器,即坐标的参照物
* ManipulationOrigin - 当前手势的中心点坐标
* Complete() - 停止捕获手势操作,即不会再触发 ManipulationDelta 事件
* DeltaManipulation.Scale - 最近一次缩放比的变化
* DeltaManipulation.Translation - 最近一次位移的变化
* CumulativeManipulation.Scale - 缩放比的累计的变化
* CumulativeManipulation.Translation - 位移的累计的变化
*
* ManipulationCompletedEventArgs
* ManipulationContainer - 手势操作的容器,即坐标的参照物
* ManipulationOrigin - 当前手势的中心点坐标
* TotalManipulation.Scale - 缩放比的总计变化
* TotalManipulation.Translation - 位移的总计变化
* IsInertial - 手势操作收尾时是否有惯性
* FinalVelocities.LinearVelocity - 如果手势操作收尾时有惯性,则此属性会返回其线性速度
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace Demo.Input.Touch
{
public partial class ManipulationDemo : PhoneApplicationPage
{
public ManipulationDemo()
{
InitializeComponent();
// 注册相关手势事件
LayoutRoot.ManipulationStarted += new EventHandler ManipulationStartedEventArgs (LayoutRoot_ManipulationStarted);
LayoutRoot.ManipulationDelta += new EventHandler ManipulationDeltaEventArgs (LayoutRoot_ManipulationDelta);
LayoutRoot.ManipulationCompleted += new EventHandler ManipulationCompletedEventArgs (LayoutRoot_ManipulationCompleted);
void LayoutRoot_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
lblMsg.Text = string.Format("手势起始点坐标 - x: {0},y: {1}", e.ManipulationOrigin.X, e.ManipulationOrigin.Y);
void LayoutRoot_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
lblMsg.Text = string.Format("当前手势的中心点坐标 - x: {0},y: {1}", e.ManipulationOrigin.X, e.ManipulationOrigin.Y);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "缩放比最近的变化:" + e.DeltaManipulation.Scale.ToString();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "位移最近的变化" + e.DeltaManipulation.Translation.ToString();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "缩放比总共的变化:" + e.CumulativeManipulation.Scale.ToString();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "位移总共的变化:" + e.CumulativeManipulation.Translation.ToString();
lblMsg.Text += Environment.NewLine;
void LayoutRoot_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
lblMsg.Text = string.Format("当前手势的中心点坐标 - x: {0},y: {1}", e.ManipulationOrigin.X, e.ManipulationOrigin.Y);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "缩放比总共的变化:" + e.TotalManipulation.Scale.ToString();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "位移总共的变化:" + e.TotalManipulation.Translation.ToString();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "手势操作收尾时是否有惯性:" + e.IsInertial.ToString();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "如果手势操作收尾时有惯性,其速度为:" + e.FinalVelocities.LinearVelocity.ToString();
}
}
}

3、演示如何通过 XNA 捕获手势操作
XNAGesture.xaml

phone:PhoneApplicationPage
x:
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="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:Design d:Design
shell:SystemTray.IsVisible="True"
Grid x:Name="LayoutRoot" Background="Transparent"
StackPanel Orientation="Vertical"
TextBlock Text="请随便做一些手势操作,以查看演示效果" /
TextBlock Name="lblMsg" TextWrapping="Wrap" /
/StackPanel
/Grid
/phone:PhoneApplicationPage

XNAGesture.xaml.cs

/*
* 演示如何在 XNA 下捕获手势操作
*
* TouchPanel - 触摸面板
* DisplayOrientation - 显示方向(Microsoft.Xna.Framework.DisplayOrientation 枚举)
* Default, LandscapeLeft, LandscapeRight, Portrait
* DisplayWidth - 宽
* DisplayHeight - 高
* IsGestureAvailable - 当前是否存在有效的手势操作
* EnabledGestures - 指定需要捕获的手势操作类型(Microsoft.Xna.Framework.Input.Touch.GestureType 枚举)
* None, Tap, DoubleTap, Hold, HorizontalDrag, VerticalDrag, FreeDrag, Pinch, Flick, DragComplete, PinchComplete
* GetCapabilities() - 返回 TouchPanelCapabilities 类型对象
* ReadGesture() - 返回 GestureSample 类型对象
*
*
* TouchPanelCapabilities - 触摸板的能力
* IsConnected - 触摸板是否有效
* MaximumTouchCount - 触摸板可以同时捕获的最大点数,即最大支持几点触摸
*
* GestureSample - 手势信息
* GestureType - 手势操作类型
* Position - 两点手势操作时,第一点的位置
* Position2 - 两点手势操作时,第二点的位置
* Delta - 两点手势操作时,第一点的位移的最近变化值
* Delta2 - 两点手势操作时,第二点的位移的最近变化值
* Timestamp - 此手势开始的时间戳(此值为系统自上次启动以来到此手势开始时所经过的时间)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Input.Touch;
namespace Demo.Input.Touch
{
public partial class XNAGesture : PhoneApplicationPage
{
public XNAGesture()
{
InitializeComponent();
// 在手势操作完成后获取相关的手势信息
LayoutRoot.ManipulationCompleted += new EventHandler ManipulationCompletedEventArgs (LayoutRoot_ManipulationCompleted);
TouchPanelCapabilities tpc = TouchPanel.GetCapabilities();
lblMsg.Text = "触摸板是否有效:" + tpc.IsConnected;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "触摸板最大支持 " + tpc.MaximumTouchCount + " 点触摸";
lblMsg.Text += Environment.NewLine;
// 指定需要捕获的手势类型(只有指定的类型才能捕获到)
TouchPanel.EnabledGestures = GestureType.Tap | GestureType.DoubleTap | GestureType.Hold | GestureType.HorizontalDrag | GestureType.VerticalDrag | GestureType.FreeDrag | GestureType.Pinch | GestureType.Flick | GestureType.DragComplete | GestureType.PinchComplete;
// 当一次手势操作完成后,获取此次手势操作包含的全部手势信息
void LayoutRoot_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
/*
* 由于一次手势操作可能会有多个手势信息,所以这里要一直输出手势信息直至没有有效的手势为止
* 这里的一次手势操作指的是手指按下到抬起后,这期间可能会有多个手势信息(比如一次手势操作可能会由多个水平移动、多个垂直移动等组成)
* 可以在每一帧当 TouchPanel.IsGestureAvailable 有效时都获取一下 TouchPanel.ReadGesture(),这样就可以即时获取到当前手势信息
*/
while (TouchPanel.IsGestureAvailable)
{
GestureSample gs = TouchPanel.ReadGesture();
lblMsg.Text += gs.GestureType.ToString() + ",";
}
}
}
}

4、演示如何响应多点触控
MutiTouch.xaml

phone:PhoneApplicationPage
x:
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="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:Design d:Design
shell:SystemTray.IsVisible="True"
Grid x:Name="LayoutRoot" Background="Transparent"
StackPanel Orientation="Vertical"
TextBlock Text="请多点触摸此屏幕,以查看演示效果" /
TextBlock Name="lblMsg" /
/StackPanel
/Grid
/phone:PhoneApplicationPage

MutiTouch.xaml.cs

/*
* 演示系统对多点触摸的支持
*
*
* TouchPanel - 触摸面板
* GetState() - 返回触摸面板上,被触摸的所有点的状态(TouchCollection 类型)
*
* TouchCollection - 触摸点的集合(TouchLocation 的集合)
*
* TouchLocation - 触摸点
* Position - 触摸点的坐标
* State - 触摸点的状态(TouchLocationState 类型)
*
* TouchLocationState - 触摸点状态(Microsoft.Xna.Framework.Input.Touch.TouchLocationState 枚举)
* Invalid - 无效
* Released - 被释放
* Pressed - 被触摸,即一个新的触摸点
* Moved - 被触摸后移动
*
*
* 注:目前只支持 4 点触摸
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using Microsoft.Xna.Framework.Input.Touch;
namespace Demo.Input.Touch
{
public partial class MutiTouch : PhoneApplicationPage
{
public MutiTouch()
{
InitializeComponent();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
void CompositionTarget_Rendering(object sender, EventArgs e)
{
this.Dispatcher.BeginInvoke(delegate
{
lblMsg.Text = "";
// 获取当前触摸点集合
TouchCollection touchLocatiOns= TouchPanel.GetState();
for (int i = ; i touchLocations.Count; i++)
{
TouchLocation touchLocation = touchLocations[i];
float x = touchLocation.Position.X;
float y = touchLocation.Position.Y;
// 显示每个触摸点的坐标及状态
this.Dispatcher.BeginInvoke(new InvokeUIWidthParameterDelegate(ShowMessage), i, x, y, touchLocation.State);
}
void ShowMessage(int i, float x, float y, TouchLocationState state)
{
lblMsg.Text += string.Format("触摸点{0}坐标 - X: {1},Y: {2},状态: {3}", i, x, y, state.ToString());
lblMsg.Text += Environment.NewLine;
private delegate void InvokeUIWidthParameterDelegate(int i, float x, float y, TouchLocationState state);
}
}

OK


   



推荐阅读
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文介绍了一款名为TimeSelector的Android日期时间选择器,采用了Material Design风格,可以在Android Studio中通过gradle添加依赖来使用,也可以在Eclipse中下载源码使用。文章详细介绍了TimeSelector的构造方法和参数说明,以及如何使用回调函数来处理选取时间后的操作。同时还提供了示例代码和可选的起始时间和结束时间设置。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • Sleuth+zipkin链路追踪SpringCloud微服务的解决方案
    在庞大的微服务群中,随着业务扩展,微服务个数增多,系统调用链路复杂化。Sleuth+zipkin是解决SpringCloud微服务定位和追踪的方案。通过TraceId将不同服务调用的日志串联起来,实现请求链路跟踪。通过Feign调用和Request传递TraceId,将整个调用链路的服务日志归组合并,提供定位和追踪的功能。 ... [详细]
  • 1简介本文结合数字信号处理课程和Matlab程序设计课程的相关知识,给出了基于Matlab的音乐播放器的总体设计方案,介绍了播放器主要模块的功能,设计与实现方法.我们将该设 ... [详细]
author-avatar
sdlzq
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有