zoukankan      html  css  js  c++  java
  • Silverlight学习笔记十八最简单的添加双击事件的实现方法

    最近在网上查了一些关于捕捉Silverlight双击事件的方法,觉得比较麻烦,于是本人狠下心来琢磨出了一种相当见的方法,可以用公共类。相当简单哦。

    步骤一、双击事件类DoubleClick.cs

    using System;
    using System.Threading;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Threading;

    namespace Wjq.SL.View.Core
    {
        public delegate void MouseLeftDoubleDownEventHandler(object sender, MouseButtonEventArgs e);
        public delegate void MouseLeftOnceDownEventHandler(object sender, MouseButtonEventArgs e);

        /// <summary>
        /// 定义了双击事件的类
        /// </summary>
        public class DoubleClick
        {
            /// <summary>
            /// 双击事件定时器
            /// </summary>
            private DispatcherTimer doubleClickTimer;

            /// <summary>
            /// 是否单击
            /// </summary>
            private bool isOnceClick;

            /// <summary>
            /// 双击事件
            /// </summary>
            public MouseLeftDoubleDownEventHandler mouseLeftDoubleDown;

            /// <summary>
            /// 单击事件
            /// </summary>
            public MouseLeftOnceDownEventHandler mouseLeftOnceDown;

            /// <summary>
            /// 拥有双击事件的UI
            /// </summary>
            private UIElement owner;

            /// <summary>
            /// 实例化DoubleClick
            /// </summary>
            /// <param name="owner">具有双击事件的UI</param>
            public DoubleClick(UIElement owner)
            {
                this.owner = owner;
                this.bindEvent();
            }

            /// <summary>
            /// 绑定事件
            /// </summary>
            private void bindEvent()
            {
                this.owner.MouseLeftButtonDown += (new MouseButtonEventHandler(this.owner_MouseLeftButtonDown));
                DispatcherTimer timer = new DispatcherTimer();
                //设置单击事件
                timer.Interval = (new TimeSpan(0, 0, 0, 0, 200));
                this.doubleClickTimer = timer;
                this.doubleClickTimer.Tick += (new EventHandler(this.doubleClickTimer_Tick));
            }

            private void doubleClickTimer_Tick(object sender, EventArgs e)
            {
                this.isOnceClick = false;
                this.doubleClickTimer.Stop();
            }

            private void owner_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                if (!this.isOnceClick)
                {
                    this.isOnceClick = true;
                    this.doubleClickTimer.Start();
                    this.mouseLeftOnceDown(sender, e);
                }
                else
                {
                    this.mouseLeftDoubleDown(sender, e);
                }
            }
        }
    }

    注:此类还定义了它的单击事件,应用时必须将俩个全部实现。

    步骤二、应用上述的公共类

    1.前台 Full.xaml

    <UserControl x:Class="Test.full"
        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"
        mc:Ignorable="d"
                 xmlns:Wjq="clr-namespace:Wjq.SL.View;assembly=Wjq.SL.View"
        d:DesignHeight="300" d:DesignWidth="400">
       
       
        <Grid x:Name="LayoutRoot" Background="White">
            <Canvas Background="AntiqueWhite" x:Name="fullscreen"></Canvas>
        </Grid>
    </UserControl>

    2.后台 Full.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Wjq.SL.View.Core;
    using System.Windows.Threading;

    namespace Test
    {
        public partial class full : UserControl
        {
            public full()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(UserControl_Loaded);
            }

            private DoubleClick MouseDoubleClick;

            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                MouseDoubleClick = new DoubleClick(this.fullscreen);
                this.MouseDoubleClick.mouseLeftDoubleDown += new MouseLeftDoubleDownEventHandler(full_mouseLeftDoubleDown);
                this.MouseDoubleClick.mouseLeftOnceDown += new MouseLeftOnceDownEventHandler(full_mouseLeftOnceDown);
            }

            private void full_mouseLeftDoubleDown(object sender, MouseButtonEventArgs e)
            {
                Application.Current.Host.Content.IsFullScreen=!Application.Current.Host.Content.IsFullScreen;
            }

      //如果不用,也得实现

            private void full_mouseLeftOnceDown(object sender, MouseButtonEventArgs e)
            {
                return;
            }

        }
    }

  • 相关阅读:
    [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
    [Angular] Using ngTemplateOutlet to create dynamic template
    [Angular] Create dynamic content with <tempalte>
    [Postgres] Create a Postgres Table
    [Typescript 2] Nullable Types
    [Ramda] Sort, SortBy, SortWith in Ramda
    [Django] Creating an app, models and database
    .NET通用权限系统快速开发框架
    MVC中使用EF(1):为ASP.NET MVC程序创建Entity Framework数据模型
    TIME_WAIT引起Cannot assign requested address报错
  • 原文地址:https://www.cnblogs.com/salam/p/1809402.html
Copyright © 2011-2022 走看看