Silverlight触摸事件:(微软建议触摸区域不小于9毫米。若手机屏幕分辨率为264DPI,那么9毫米就是94像素。)

  silverlight支持两种不同的编程接口来支持多点触摸,简单分为底层接口和高层接口。底层接口是基于静态的Touch.FrameReported事件,不包含手势操作。高层接口直接由UTElement中定义的三个事件组成:ManipulationStarted、Manipulation-Delta和ManipulationCompleted。这些事件统称为Manipulation事件,把多个手指的交互操作合并成缩放和移动两个因子。

  1、silverlight底层触摸接口核心是Touchpoint类,Touchpoint的每个实例分别代表触摸屏幕的每个手指。Touchpoint包括4个只读属性:

    *Action:Touchpoint枚举类型,包含Down、Move、Up三个成员。

    *Position:Point类型,参考(reference元素),表示相对于特定元素左上角的位置。

    *Size :Size类型,代表接触面积,不返回有用的值。

    *TouchDevice:TouchDevice类型。包含Id(int类型,区分不同的手指,从Down到Up所有事件中,一个特定的手指只有一个唯一的Id)和DirectlyOver(类型为ULElement,手             指下的最顶层元素)两个只读属性。

    要使用底层触摸接口,需要为静态的Touch.FrameReported事件注册一个事件处理程序:

           Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);

    Eg 1:

      MainPage.xaml(节选)

      <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <TextBlock x:Name="txtblk" Text="Hello,Windows Phone 7!" Padding="0 34" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>

      </Grid>

      <!--如果不设置HorizontalAlignment="Center" VerticalAlignment="Center",那么默认情况下,属性值为stretch。也就是说textblock充满整个网络--!>

      MainPage.xaml.cs(节选)

      publicpartialclassMainPage : PhoneApplicationPage

       {

        // Constructor

        Random rand = newRandom();

        Brush originalBrush;

        public MainPage()

        {

        InitializeComponent();

        originalBrush = txtblk.Foreground;

        Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);//为静态的Touch.FrameReported注册一个touch事件处理程序

        }

        void Touch_FrameReported(object sender, TouchFrameEventArgs e)

        {

          TouchPoint primaryTouchPoint = e.GetPrimaryTouchPoint(null);//传递null值,获取屏幕左上角与触摸点的相对位置;也可以传递一个参考元素。返回的集合中的TouchPoint对象有一个Position属性,表示触摸点与该参考对象的相对位置。

          if (primaryTouchPoint != null && primaryTouchPoint.Action == TouchAction.Down)//如果有一个手指触摸当前屏幕时发生

          {

            if (primaryTouchPoint.TouchDevice.DirectlyOver == txtblk)//DirectlyOver表示位于手指下的元素

             {

                txtblk.Foreground =newSolidColorBrush(Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256)));

              //Silverlight中的Color结构体没有从红、绿、蓝值设置颜色的构造函数。FormArgb静态方法可以基于alpha(透明度)、红、绿、蓝直接创建Color对象。

              }

            else

            {

              txtblk.Foreground = originalBrush;

            }

          }

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

             {

               base.OnNavigatedTo(e);

               DispatcherTimer dt = newDispatcherTimer();

                dt.Interval =TimeSpan.FromSeconds(1);

                dt.Tick +=newEventHandler(dt_Tick);

                dt.Start();

           }

                     ********************************************************

        //高层接口实现相同功能。事件注册在mainpage上

            protectedoverridevoid OnManipulationStarted(ManipulationStartedEventArgs e)

            {

               if (e.OriginalSource == txtblk)

                {

                   Color clr = Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256));

                    txtblk.Foreground =newSolidColorBrush(clr);

                }

               else

                {

                    txtblk.Foreground = originalBrush;

         }

               e.Complete();//不需要再处理该手指所涉及的Manipulation事件了。

              base.OnManipulationStarted(e);

            }

            *********************************

    //以下是直接将事件注册在txtblk上,不提倡

         private void txtblk_ManipulationStarted(object sender, ManipulationStartedEventArgs e)

         {

            TextBlock txtblk = sender as TextBlock;

           Color clr = Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256));

            txtblk.Foreground = new SolidColorBrush(clr);

           e.Complete();

         }

    }

}

  }

 

 

 

    

  

posted on 2012-09-27 10:43  阳光的七夜  阅读(259)  评论(0编辑  收藏  举报