微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

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

       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 。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐