Error Cannot convert lambda expression in subscribe for an IObservable<Point>

ashutosh raina picture ashutosh raina · Feb 26, 2012 · Viewed 7.9k times · Source

i am trying to implement a standard drag and drop image in wpf using Rx.

var mouseDown = from evt in Observable.FromEventPattern<MouseButtonEventArgs>(image, "MouseLeftButtonDown")
                          select evt.EventArgs.GetPosition(image);

            var mouseUp = Observable.FromEventPattern<MouseButtonEventArgs>(this, "MouseLeftButtonUp");

            var mouseMove = from evt in Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove")
                            select evt.EventArgs.GetPosition(this);

            var q = from startLocation in mouseDown
                    from endLocation in mouseMove.TakeUntil(mouseUp)
                    select new Point 
                    {
                        X = endLocation.X - startLocation.X,
                        Y = endLocation.Y - startLocation.Y
                    };

            q.ObserveOn(SynchronizationContext.Current).Subscribe(point =>
            {
                Canvas.SetLeft(image, point.X);
                Canvas.SetTop(image, point.Y);
            });

i get the error Error Cannot convert lambda expression to type 'System.IObserver<System.Windows.Point>' because it is not a delegate type

what am i missing ?

Answer

Asti picture Asti · Mar 6, 2012

The namespace System.Reactive.Linq contains the static class Observable which defines all the extension methods for common reactive combinators. It resides in System.Reactive.dll

The extension methods for IObservable<T>.Subscribe such as Subscribe(onNext), Subscribe(onNext, onError) are however defined in mscorlib in the static class System.ObservableExtensions.

tl;dr:

  • For Rx/Observable extension methods you need to import System.Reactive.Linq = using System.Reactive.Linq;
  • For Subscribe overloads you need to import System = using System;