Reactive Extension - Observable.FromEvent类型转换

时间:2011-05-10 11:39:32

标签: linq system.reactive

我正在尝试使用实验室的反应式扩展程序,但无法使用FromEvent API进行注册(我收到编译时异常)。我希望在文本框控件上调用textchanged属性时接收事件,有人可以通过FromEvent API建议我在转换时出错吗?我有些怎么不能输入或从TextChangeEventArgs / EventHandler转换为EventHandler - 请告诉我正确的方法。感谢。

        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();

3 个答案:

答案 0 :(得分:11)

试试这个:

var subscription = Observable.FromEventPattern<TextChangedEventArgs>(txt, "TextChanged"); 

答案 1 :(得分:3)

尝试:

var subscription = Observable.FromEvent(h => txt.TextChanged += h,
                                        h => txt.TextChanged -= h);

答案 2 :(得分:0)

试试这个(修改:如果您定位 Windows Phone Reactive API ):

var subscription = Observable.FromEvent<TextChangedEventArgs>(txt, "TextChanged");