在Silverlight应用程序和客户进行交互工作的时候可以不用写后台代码而通过Xaml代码来实现,在本文我们将学习了解Trigger触发器。
Trigger触发器:引发动作的因素,比如鼠标点击、键盘输入、鼠标双击、键盘Enter键敲入、鼠标中键滚动等等,这些都是触发动作交互的条件。
Trigger分为以下两类:
一、系统定义好的如EventTrigger、PropertyTrigger等。
二、用户自定义的Trigger,例如在SL4中是没有鼠标双击事件的,这时我们可以新建一个DoubleClickTrriger,通过定时器检测当点击页面同一个地方的时间间隔小于300毫秒的都属于鼠标触发动作。
EventTrigger主要是指定触发的事件名称,如下例是在MouseLeftButtonUp的时候触发ChangePropertyAction动作,在本例中不作详细讲述:
自定义Trigger:本实例中我们自定义一个翻页的触发器,它通过在指定对象上按下按钮,然后滑动鼠标向左或者向右移动然后放开鼠标,自动检测是向左翻 页还是向右翻页。自定义Trigger和Behavior一样只需要重写OnAttached和OnDetaching方法即可,自定义Trigger需 要继承于TriggerBase<T>类。
- 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是触发器被触发时执行的动作,下一篇会详细讲述,其代码如下:
- <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 。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。