Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器
2012-12-11 16:04
489 查看
在Silverlight应用程序和客户进行交互工作的时候可以不用写后台代码而通过Xaml代码来实现,在本文我们将学习了解Trigger触发器。
Trigger触发器:引发动作的因素,比如鼠标点击、键盘输入、鼠标双击、键盘Enter键敲入、鼠标中键滚动等等,这些都是触发动作交互的条件。
Trigger分为以下两类:
一、系统定义好的如EventTrigger、PropertyTrigger等。
二、用户自定义的Trigger,例如在SL4中是没有鼠标双击事件的,这时我们可以新建一个DoubleClickTrriger,通过定时器检测当点击页面同一个地方的时间间隔小于300毫秒的都属于鼠标触发动作。
EventTrigger主要是指定触发的事件名称,如下例是在MouseLeftButtonUp的时候触发ChangePropertyAction动作,在本例中不作详细讲述:
自定义Trigger:本实例中我们自定义一个翻页的触发器,它通过在指定对象上按下按钮,然后滑动鼠标向左或者向右移动然后放开鼠标,自动检测是向左翻页还是向右翻页。自定义Trigger和Behavior一样只需要重写OnAttached和OnDetaching方法即可,自定义Trigger需要继承于TriggerBase<T>类。
自定义Trigger代码如下:
自定义的Action代码如下,注意Action是触发器被触发时执行的动作,下一篇会详细讲述,其代码如下:
在主页面调用的时候其Xaml代码如下:
在Xaml.cs代码如下:
最后如需源码请点击 SLTrigger.zip 下载,需要引用System.Windows.Interactivity.dll 。
Trigger触发器:引发动作的因素,比如鼠标点击、键盘输入、鼠标双击、键盘Enter键敲入、鼠标中键滚动等等,这些都是触发动作交互的条件。
Trigger分为以下两类:
一、系统定义好的如EventTrigger、PropertyTrigger等。
二、用户自定义的Trigger,例如在SL4中是没有鼠标双击事件的,这时我们可以新建一个DoubleClickTrriger,通过定时器检测当点击页面同一个地方的时间间隔小于300毫秒的都属于鼠标触发动作。
EventTrigger主要是指定触发的事件名称,如下例是在MouseLeftButtonUp的时候触发ChangePropertyAction动作,在本例中不作详细讲述:
<Rectangle Width="300"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonUp"> <ei:ChangePropertyAction /> </i:EventTrigger> </i:Interaction.Triggers> </Rectangle>
自定义Trigger:本实例中我们自定义一个翻页的触发器,它通过在指定对象上按下按钮,然后滑动鼠标向左或者向右移动然后放开鼠标,自动检测是向左翻页还是向右翻页。自定义Trigger和Behavior一样只需要重写OnAttached和OnDetaching方法即可,自定义Trigger需要继承于TriggerBase<T>类。
自定义Trigger代码如下:
public class PaggerTrigger : TriggerBase<UIElement> { private Point _downPosition; protected override void OnAttached() { base.OnAttached(); //加载事件 AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown); AssociatedObject.MouseLeftButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp); } void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { UIElement element = sender as UIElement; _downPosition = e.GetPosition(element); } void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { UIElement element = sender as UIElement; Point position = e.GetPosition(element); double X_Content = position.X - _downPosition.X ; PageEnum pageEnum = PageEnum.PageLeft; if (Math.Abs(X_Content) > 10) { if (X_Content > 0) { pageEnum = PageEnum.PageRight; } else { pageEnum = PageEnum.PageLeft; } InvokeActions(pageEnum); } } protected override void OnDetaching() { base.OnDetaching(); //卸载事件 AssociatedObject.MouseLeftButtonDown -= new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown); AssociatedObject.MouseLeftButtonUp -= new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp); } } /// <summary> /// 指示翻页方向枚举 /// </summary> public enum PageEnum { /// <summary> /// 左翻页 /// </summary> PageLeft, /// <summary> /// 右翻页 /// </summary> PageRight }
自定义的Action代码如下,注意Action是触发器被触发时执行的动作,下一篇会详细讲述,其代码如下:
//动作 public class InvokeAction : TargetedTriggerAction<UIElement> { protected override void Invoke(object parameter) { if (ToInvoke != null) { ToInvoke(parameter, new RoutedEventArgs() { }); } } public delegate void Handler(object sender, RoutedEventArgs e); public event Handler ToInvoke; }
在主页面调用的时候其Xaml代码如下:
<UserControl x:Class="SLTrigger.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" xmlns:me="clr-namespace:SLTrigger" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <Image Width="300" Source="/SLTrigger;component/chun.jpg" Margin="50,127,50,-16"> <i:Interaction.Triggers> <me:PaggerTrigger> <me:InvokeAction ToInvoke="PageClickHandler" /> </me:PaggerTrigger> </i:Interaction.Triggers> </Image> </Grid> </UserControl>
在Xaml.cs代码如下:
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } //实现指定动作时出发的事件处理程序 private void PageClickHandler(object sender, RoutedEventArgs e) { PageEnum pageEnum = (PageEnum)sender; string info = string.Empty; if (pageEnum == PageEnum.PageLeft) { info = "向左翻页"; } else if (pageEnum == PageEnum.PageRight) { info = "向右翻页"; } MessageBox.Show(info); } }
最后如需源码请点击 SLTrigger.zip 下载,需要引用System.Windows.Interactivity.dll 。
相关文章推荐
- Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器
- Silverlight实用窍门系列:61.Silverlight中的Trigger触发器,自定义翻页触发器
- Silverlight实用窍门系列:60.Silverlight中自定义Behavior,实现图片动画渐变Behavior
- Silverlight实用窍门系列:60.Silverlight中自定义Behavior,实现图片动画渐变Behavior
- Silverlight实用窍门系列:72.Silverlight的Implicit Style、自定义主题皮肤、系统主题皮肤
- Silverlight实用窍门系列:72.Silverlight的Implicit Style、自定义主题皮肤、系统主题皮肤 推荐
- Silverlight实用窍门系列:72.Silverlight的Implicit Style、自定义主题皮肤、系统主题皮肤
- Silverlight实用窍门系列:62.Silverlight中的Action动作TargetedTriggerAction、TriggerAction
- Silverlight实用窍门系列:63.Silverlight中的Command,自定义简单Command
- Silverlight实用窍门系列:62.Silverlight中的Action动作TargetedTriggerAction、TriggerAction 推荐
- Silverlight实用窍门系列:63.Silverlight中的Command,自定义简单Command 推荐
- Silverlight实用窍门系列:68.Silverlight的资源字典ResourceDictionary
- Silverlight实用窍门系列:51.Silverlight页面控件的放大缩小、Silverlight和Html控件的互相操作
- Silverlight实用窍门系列:52.Silverlight中的MVVM框架极速入门(以MVVM Light Toolkit为例)
- Silverlight实用窍门系列:40.Silverlight中捕捉视频,截图保存到本地
- Silverlight实用窍门系列:42.读取拖动到控件上的外部txt和jpg文件,多外部文件的拖动【附带实例源码】
- Silverlight实用窍门系列:43.Silverlight从ListBox拖拽图标到另一ListBox
- Silverlight实用窍门系列:1.Silverlight读取外部XML加载配置---(使用WebClient读取XAP包同目录下的XML文件))【附带实例源码】
- Silverlight实用窍门系列:6.Silverlight弹出窗口以及DataGrid分页【附带实例源码】
- Silverlight实用窍门系列:14.Visifire图表控件的使用一(图表的创建和基础使用)【附带源码实例】