且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器

更新时间:2022-08-17 08:17:16

 在Silverlight应用程序和客户进行交互工作的时候可以不用写后台代码而通过Xaml代码来实现,在本文我们将学习了解Trigger触发器。

       Trigger触发器:引发动作的因素,比如鼠标点击、键盘输入、鼠标双击、键盘Enter键敲入、鼠标中键滚动等等,这些都是触发动作交互的条件。

       Trigger分为以下两类:

    一、系统定义好的如EventTrigger、PropertyTrigger等。

    二、用户自定义的Trigger,例如在SL4中是没有鼠标双击事件的,这时我们可以新建一个DoubleClickTrriger,通过定时器检测当点击页面同一个地方的时间间隔小于300毫秒的都属于鼠标触发动作。

   EventTrigger主要是指定触发的事件名称,如下例是在MouseLeftButtonUp的时候触发ChangePropertyAction动作,在本例中不作详细讲述: 

 


  1. <Rectangle Width="300"
  2.     <i:Interaction.Triggers> 
  3.         <i:EventTrigger EventName="MouseLeftButtonUp"
  4.             <ei:ChangePropertyAction /> 
  5.         </i:EventTrigger> 
  6.     </i:Interaction.Triggers> 
  7. </Rectangle> 

        自定义Trigger:本实例中我们自定义一个翻页的触发器,它通过在指定对象上按下按钮,然后滑动鼠标向左或者向右移动然后放开鼠标,自动检测是向左翻 页还是向右翻页。自定义Trigger和Behavior一样只需要重写OnAttached和OnDetaching方法即可,自定义Trigger需 要继承于TriggerBase<T>类。

        自定义Trigger代码如下:

 


  1. public class PaggerTrigger : TriggerBase<UIElement> 
  2.     private Point _downPosition; 
  3.  
  4.     protected override void OnAttached() 
  5.     { 
  6.         base.OnAttached(); 
  7.         //加载事件 
  8.         AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown); 
  9.         AssociatedObject.MouseLeftButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp); 
  10.     } 
  11.  
  12.     void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
  13.     { 
  14.         UIElement element = sender as UIElement; 
  15.         _downPosition = e.GetPosition(element); 
  16.     } 
  17.  
  18.     void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
  19.     { 
  20.         UIElement element = sender as UIElement; 
  21.         Point position = e.GetPosition(element); 
  22.         double X_Content =  position.X - _downPosition.X ; 
  23.         PageEnum pageEnum = PageEnum.PageLeft; 
  24.         if (Math.Abs(X_Content) > 10) 
  25.         { 
  26.             if (X_Content > 0) 
  27.             { 
  28.                 pageEnum = PageEnum.PageRight; 
  29.             } 
  30.             else 
  31.             { 
  32.                 pageEnum = PageEnum.PageLeft; 
  33.             } 
  34.             InvokeActions(pageEnum); 
  35.         } 
  36.     } 
  37.  
  38.     protected override void OnDetaching() 
  39.     { 
  40.         base.OnDetaching(); 
  41.         //卸载事件 
  42.         AssociatedObject.MouseLeftButtonDown -= new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown); 
  43.         AssociatedObject.MouseLeftButtonUp -= new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp); 
  44.     } 
  45.  
  46. /// <summary> 
  47. /// 指示翻页方向枚举 
  48. /// </summary> 
  49. public enum PageEnum 
  50. {  
  51.     /// <summary> 
  52.     /// 左翻页 
  53.     /// </summary> 
  54.     PageLeft, 
  55.  
  56.     /// <summary> 
  57.     /// 右翻页 
  58.     /// </summary> 
  59.     PageRight 

       自定义的Action代码如下,注意Action是触发器被触发时执行的动作,下一篇会详细讲述,其代码如下:

 


  1. //动作 
  2. public class InvokeAction : TargetedTriggerAction<UIElement> 
  3.     protected override void Invoke(object parameter) 
  4.     { 
  5.         if (ToInvoke != null
  6.         { 
  7.             ToInvoke(parameter, new RoutedEventArgs() { }); 
  8.         } 
  9.     } 
  10.  
  11.     public delegate void Handler(object sender, RoutedEventArgs e); 
  12.     public event Handler ToInvoke; 
  13.  

        在主页面调用的时候其Xaml代码如下:

 


  1. <UserControl x:Class="SLTrigger.MainPage" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
  7.           xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"   
  8.              xmlns:me="clr-namespace:SLTrigger" 
  9.     mc:Ignorable="d" 
  10.     d:DesignHeight="300" d:DesignWidth="400"
  11.  
  12.     <Grid x:Name="LayoutRoot" Background="White"
  13.         <Image Width="300" Source="/SLTrigger;component/chun.jpg" Margin="50,127,50,-16"
  14.             <i:Interaction.Triggers> 
  15.                 <me:PaggerTrigger> 
  16.                     <me:InvokeAction ToInvoke="PageClickHandler" /> 
  17.                 </me:PaggerTrigger> 
  18.             </i:Interaction.Triggers> 
  19.         </Image> 
  20.     </Grid> 
  21. </UserControl> 

        在Xaml.cs代码如下:

 


  1. public partial class MainPage : UserControl 
  2.     public MainPage() 
  3.     { 
  4.         InitializeComponent(); 
  5.     } 
  6.     //实现指定动作时出发的事件处理程序 
  7.     private void PageClickHandler(object sender, RoutedEventArgs e) 
  8.     { 
  9.         PageEnum pageEnum = (PageEnum)sender; 
  10.         string info = string.Empty; 
  11.         if (pageEnum == PageEnum.PageLeft) 
  12.         { 
  13.             info = "向左翻页"
  14.         } 
  15.         else if (pageEnum == PageEnum.PageRight) 
  16.         { 
  17.             info = "向右翻页"
  18.         } 
  19.         MessageBox.Show(info); 
  20.     } 

        最后如需源码请点击 SLTrigger.zip 下载,需要引用System.Windows.Interactivity.dll 。



本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/827187