• How to Implement Double Click


    Silverlight currently has full mouse support for single click. However, double click is a another story. In this tip I will show you how to implement double click. You can apply this technique for an individual control or even your entire page.

    The key thing to do is to start a DispatcherTimer timer once a left mouse click event has been received. If another mouse click is intercepted before the double click time interval has passed then a double click has occurred. This interval is typically set to be around 200 milliseconds. Once 200 milliseconds has passed the timer is stopped and disabled until another mouse click is received.

    To start, let’s create our timer and add a listener for the MouseLeftButtonDown event.

    DispatcherTimer _doubleClickTimer;
    Image _lastImage = null;
     
    public Page()
    {
        InitializeComponent();
       
        _doubleClickTimer = new DispatcherTimer();
        _doubleClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 200);
        _doubleClickTimer.Tick += new EventHandler(DoubleClick_Timer);
     
        this.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
    }
     
    // too much time has passed for it to be a double click.            
    void DoubleClick_Timer(object sender, EventArgs e)
    {
        _doubleClickTimer.Stop();
    }

    Now, in the Page_MouseLeftButtonDown() method we:

    1. Check if the timer is enabled.
    2. If it is enabled already, then we have already clicked once and have made a double click.
    3. If is it not enabled start the timer.
    void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (_doubleClickTimer.IsEnabled)
        {
            // a double click has occured
            _doubleClickTimer.Stop();
     
            Image catImg = new Image();
            Uri uri = new Uri("cat.png", UriKind.Relative);
            ImageSource imgSource = new
                System.Windows.Media.Imaging.BitmapImage(uri);
            catImg.Source = imgSource; 
     
            catImg.SetValue(Canvas.LeftProperty,(double) e.GetPosition(LayoutRoot).X-90);
            catImg.SetValue(Canvas.TopProperty, (double)e.GetPosition(LayoutRoot).Y-113);
     
            if(null != _lastImage)
                MainCanvas.Children.Remove(_lastImage);
            
            MainCanvas.Children.Add(catImg);
            _lastImage = catImg;
     
        }
        else
        {
            _doubleClickTimer.Start();
        }
    }
    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    Half Nice Years Gym
    LCM from 1 to n
    Educational Codeforces Round 70 (Rated for Div. 2)
    Rating(概率DP) HDU
    Josephina and RPG(概率DP) ZOJ
    数据结构实验之串二:字符串匹配(字符串哈希)
    点分治——入门学习笔记
    使用ASP.NET Core 3.x 构建 RESTful API P15 处理故障
    使用ASP.NET Core 3.x 构建 RESTful API P13 P14 获取父子关系的资源
    使用ASP.NET Core 3.x 构建 RESTful API P11 P12 ActionResult of T 以及 AutoMapper
  • 原文地址:https://www.cnblogs.com/starcrm/p/1574186.html
Copyright © 2020-2023  润新知