えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

2010年7月10日 #

Reactive Extensions for .NET (Rx) の トリプルクリック

Reactive Extensions for .NET (Rx) の トリプルクリック を作ってみた。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

 

namespace TrippleClick

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

 

            var trippleClick = GetMultiClick(button1, 3, 1000);

 

            trippleClick.Subscribe(x => System.Diagnostics.Debug.WriteLine("TrippleClick"));

        }

 

        private IObservable<IEvent<RoutedEventArgs>> GetMultiClick(Button button, int clickCount, int speed)

        {

            Queue<DateTime> timelines = new Queue<DateTime>();

 

            var trippleClick = Observable.FromEvent<RoutedEventArgs>(button, "Click")

                .Do(e =>

                    {

                        timelines.Enqueue(DateTime.Now);

                        if (timelines.Count > clickCount) timelines.Dequeue();

                    }

                )

                .Where(e =>

                    (timelines.Count == clickCount &&

                    (timelines.Last() - timelines.First()).TotalMilliseconds <= speed))

                .Do(e => timelines.Clear());

            return trippleClick;

        }

    }

}

 

posted @ 16:39 | Feedback (0)