Playing Around with System.Reactive Again

August 17, 2009

Further to my previous post, Erik Meijer commented that an overload to the FromEvent method is available to convert from generic event handlers to the specific ones used by much of the BCL. So the code from the previous post would look something like this now:

        #region Ctor
        public MainPage()
        {
            InitializeComponent();

            var mouseEventSource = Observable.FromEvent<MouseEventHandler, MouseEventArgs>(
                eh => new MouseEventHandler(eh),    // Conversion
                eh => this.MouseMove += eh,         // Subscribe
                eh => this.MouseMove -= eh);        // Unsubscribe

            _mouseMoveSubscription = mouseEventSource.Subscribe(OnMouseMove);
        }
        #endregion

        private void OnMouseMove(Event<MouseEventArgs> e)
        {
            var position = e.EventArgs.GetPosition(this);

            Debug.WriteLine("Mouse moved.  Position: {0}.", position);
        }

This solves one of my major gripes. Exciting times!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: