Silverlight里实现DoubleClick

最近在使用SL的时候发现里面没有支持双击事件,虽然可以写代码实现,但是每次都要写一段处理代码很麻烦,于是把它封装成了一个类方便调用,粘上来共享一下

 

//  *** 实现 *** 
using System;  
using System.Net;  
 using System.Windows;  
 using System.Windows.Controls;  
 using System.Windows.Documents;  
 using System.Windows.Ink;  
 using System.Windows.Input;  
 using System.Windows.Media;  
 using System.Windows.Media.Animation;  
 using System.Windows.Shapes;  
 using System.Collections.Generic;  
    
 
 namespace DoubleClickTest  
 {  
     public class DoubleClick  
     {  
         int Interval;  
        Dictionary<UIElement, TimeSpan> ControlMgr = new Dictionary<UIElement, TimeSpan>();  
    
         /// <summary> 
         /// DoubleClickEvent  
         /// </summary>  
         public event EventHandler<MouseButtonEventArgs> DoubleClickEvent = null;  
    
         /// <summary>  
         /// Construct  
         /// </summary>  
         /// <param name="interval"></param>  
         public DoubleClick(int interval = 200)  
         {  
             this.Interval = interval;  
         }  
    
         /// <summary>  
         /// Attach  
         /// </summary>  
         /// <param name="control"></param>  
         public void Attach(UIElement ctrl)  
         {  
             if (!IsExist(ctrl))  
             {  
                 this.ControlMgr[ctrl] = new TimeSpan(0);  
                 ctrl.MouseLeftButtonDown += new MouseButtonEventHandler(control_MouseLeftButtonDown);  
             }  
         }  
    
         /// <summary>  
         /// Deatch  
         /// </summary>  
         /// <param name="control"></param>  
         public void Deatch(UIElement ctrl)  
         {  
             if (IsExist(ctrl))  
             {  
                 ctrl.MouseLeftButtonDown -= new MouseButtonEventHandler(control_MouseLeftButtonDown);  
                 this.ControlMgr.Remove(ctrl);  
             }  
         }  
    
         void control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
         {  
             UIElement ctrl = sender as UIElement;  
             if (ctrl != null)  
             {  
                 TimeSpan now = new TimeSpan(DateTime.Now.Ticks);  
                 if ((now - ControlMgr[ctrl]).TotalMilliseconds <= Interval)  
                 {  
                     if (DoubleClickEvent != null)  
                     {  
                         DoubleClickEvent(sender, e);  
                     }  
                 }  
                 ControlMgr[ctrl] = now;  
             }  
        }  
 
   
         bool IsExist(UIElement control)  
         {  
            TimeSpan result;  
             return ControlMgr.TryGetValue(control, out result);  
         }  
     }  

演示

<UserControl x:Class="DoubleClickTest.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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <Grid x:Name="LayoutRoot" Background="White">
        <Ellipse Height="68" HorizontalAlignment="Left" Margin="35,23,0,0" Name="ellipse1" Stroke="Black" StrokeThickness="20" VerticalAlignment="Top" Width="102" />
        <TextBlock Height="46" HorizontalAlignment="Left" Margin="35,219,0,0" Name="textBlock1" Text="TextBlock" FontSize="22"  VerticalAlignment="Top" Width="141" />
        <Rectangle Height="64" HorizontalAlignment="Left" Margin="35,125,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="20" VerticalAlignment="Top" Width="122" />
    </Grid>
</UserControl>
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;
 
 
namespace DoubleClickTest
{
    public partial class MainPage : UserControl
    {
        /// <summary>
        /// DoubleClick object
        /// </summary>
        DoubleClick DoubleClickHandle = new DoubleClick();
 
        public MainPage()
        {
            InitializeComponent();
 
            // 注册控件的双击事件
            DoubleClickHandle.Attach(textBlock1);
            DoubleClickHandle.Attach(ellipse1);
            DoubleClickHandle.Attach(rectangle1);
 
            // 双击事件
            DoubleClickHandle.DoubleClickEvent += (s, e) =>
            {
                MessageBox.Show(s.GetType().FullName);
            };
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值