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();
Try this:
var subscription = Observable.FromEventPattern<TextChangedEventArgs>(txt, "TextChanged");
Try:
var subscription = Observable.FromEvent(h => txt.TextChanged += h,
h => txt.TextChanged -= h);
Try this (edit: if you are targeting Windows Phone Reactive API):
var subscription = Observable.FromEvent<TextChangedEventArgs>(txt, "TextChanged");
精彩评论