Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive Extension - Observable.FromEvent typecast conversion

I am trying to play with the reactive extension hands on lab but could not register using the FromEvent API (I am getting compile time exception). I want to receive events when the textchanged property is invoked on the text box control, can someone suggest what am i doing wrong on the conversion via FromEvent API? I some how cannot type cast or convert from TextChangeEventArgs/EventHandler to EventHandler - please show me the right way to do it. Thanks.

        internal delegate void TextChangedEventArgs(object sender, EventArgs e);
        var txt = new TextBox();

        var form = new Form
                      {
                          Controls = { txt }
                      };


        var subscription = Observable.FromEvent<EventHandler, TextChangedEventArgs>(
           x => (a, b) => new EventHandler(), ??????
           x => txt.TextChanged += x, 
           x => txt.TextChanged -= x
           );

       IDisposable eventSubscription = subscription.Subscribe(
           x => Console.WriteLine("Subscriber Got " + x),
           x => Console.WriteLine("An exception has occured" + x),
           () => Console.WriteLine("Action completed"));

        Console.WriteLine("in here");
       using (new CompositeDisposable(eventSubscription)) { Application.Run(form); }

        Console.ReadKey();
like image 704
Rohit Sharma Avatar asked May 10 '11 11:05

Rohit Sharma


2 Answers

Try this:

var subscription = Observable.FromEventPattern<TextChangedEventArgs>(txt, "TextChanged"); 
like image 66
Mark Good Avatar answered Nov 14 '22 10:11

Mark Good


Try:

var subscription = Observable.FromEvent(h => txt.TextChanged += h,
                                        h => txt.TextChanged -= h);
like image 31
James Hay Avatar answered Nov 14 '22 10:11

James Hay