1

I've started playing around with the Reactive Extensions for .NET using the official beginner's guide, but a lot of the method overloads were not working for me (I've referenced System.Reactive). I am not sure whether that is because the how to guide is outdated or it has some other reason (if I'm missing some assemblies, pls tell me). Here is the original and altered code:

using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows.Forms;

namespace RxFormsSandbox
{
    class Program
    {
        static void Main(string[] arguments)
        {
            var textBox = new TextBox();
            var form = new Form { Controls = { textBox } };

            // MY CODE (WORKING)

            var mouseMoves = Observable.FromEvent<MouseEventHandler, MouseEventArgs>(
                handlerAction => (_, args) => handlerAction(args),
                handler => form.MouseMove += handler,
                handler => form.MouseMove -= handler);

            var input = Observable.FromEvent<EventHandler, EventArgs>(
                handlerAction => (_, args) => handlerAction(args),
                handler => textBox.TextChanged += handler,
                handler => textBox.TextChanged -= handler);

            var mouseMovesSubscription = mouseMoves.Select(x => x.Location.ToString()).Subscribe(Console.WriteLine);
            var inputSubscription = input.Subscribe(x => Console.WriteLine(textBox.Text)); // can I do this without directly accessing 'label' (via sender)
            // END OF MY CODE

            // ORIGINAL CODE AS PROVIDED IN THE SAMPLE

//            var mouseMoves = Observable.FromEvent<MouseEventArgs>(form, "MouseMove");
//            var input = Observable.FromEvent<MouseEventArgs>(textBox, "TextChanged");
//
//            var mouseMovesSubscription = mouseMoves.Select(x => x.EventArgs.Location.ToString()).Subscribe(Console.WriteLine);
//            var inputSubscription = input.Subscribe(x => Console.WriteLine(((TextBox)x.Sender).Text));
            // END OF ORIGINAL SAMPLE CODE


            using (new CompositeDisposable(mouseMovesSubscription, inputSubscription))
            {
                Application.Run(form);
            }
        }
    }
}

With the premise, that the method's signatures has been indeed changed between releases, there are a couple of thing that bug me about my solution and I'd like to ask whether there is a better way:

  1. My rewritten version is significantly longer and I consider is less expressive. Is there a way to make it shorter (ideally without introducing magic strings)?
  2. input.Subscribe(x => Console.WriteLine(textBox.Text)); is not great, because it references outside code. I'd like to somehow access it via the 'sender' property.

My assembly comes from NuGet and its version is 1.0.10621.0.

Jan Kratochvil
  • 2,307
  • 1
  • 21
  • 39

1 Answers1

3

If you want to mimic the old way, you can use the FromEventPattern method:

var mouseMoves = Observable.FromEventPattern<MouseEventArgs>(form, "MouseMove"); 

The only downside of doing it this way is that you lose the type safety that you get when using the FromEvent method. (FromEventPattern also has an overload that uses delegates, but that deals with EventHandler<T> but MouseMove is pre .Net 2.0)

As for your second point, why is "referencing outside code" a bad thing? Closures are very much part of the C# language and there's really no reason to avoid them. In fact, in your code in the event subscription code you're doing it as well (capturing the form):

var mouseMoves = Observable.FromEvent<MouseEventHandler, MouseEventArgs>(
               handlerAction => (_, args) => handlerAction(args),
               handler => form.MouseMove += handler,
               handler => form.MouseMove -= handler);
Community
  • 1
  • 1
BFree
  • 102,548
  • 21
  • 159
  • 201
  • 1
    You can use the simpler overload of `FromEventPattern` like so: `var mouseMoves = Observable.FromEventPattern(handler => form.MouseMove += handler, handler => form.MouseMove -= handler);`. – Enigmativity Jul 06 '12 at 23:40
  • For some lengthier guidance on Rx and Events check out http://introtorx.com/Content/v1.0.10621.0/04_CreatingObservableSequences.html#FromEvent – Lee Campbell Jul 26 '12 at 09:41