Input(輸入)之捕獲 UIElement 之外的觸控操作, Silverlight 方式捕獲手勢操作, XNA 方式捕獲手勢操作, 多點觸控
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之裝置
捕獲 UIElement 之外的觸控操作
Silverlight 方式捕獲手勢操作
XNA 方式捕獲手勢操作
多點觸控
樣本
1、示範如何捕獲 UIElement 之外的觸控操作
OutsideCapture.xaml
<phone:PhoneApplicationPage x:Class="Demo.Input.Touch.OutsideCapture" 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:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <Rectangle Name="rect" Width="100" Height="100" 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(); } } }