35

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 ?

ashutosh raina
  • 9,228
  • 12
  • 44
  • 80

3 Answers3

60

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;
Asti
  • 12,447
  • 29
  • 38
24

To make this a clearer answer based on @Gideon Engelberths comment 5th down in the question I was missing the 'using System;' using directive in my class:

using System.Reactive.Linq;
using System;

Which then fixed up the compiler issue. Thanks Gideon.

The Senator
  • 5,181
  • 2
  • 34
  • 49
2

I just stumbled across this question and wanted to add, that I had to add more References to the Project.
The Reference to System.Reactive.Linq alone was not sufficient in my case.

After adding those three the compiler issue was solved:

  • System.Reactive.Core
  • System.Reactive.Interfaces
  • System.Reactive.Linq

Thanks to @Gideon Engelberths comment and The Senator's answer.

Luqqu
  • 186
  • 1
  • 9