1

我已经开始使用官方初学者指南来玩弄 .NET 的 Reactive Extensions 了,但是很多方法重载对我不起作用(我参考了 System.Reactive)。我不确定这是因为指南已过时还是有其他原因(如果我缺少一些程序集,请告诉我)。这是原始代码和更改后的代码:

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

前提是,方法的签名确实在版本之间发生了变化,有几件事让我对我的解决方案感到困惑,我想问一下是否有更好的方法:

  1. 我重写的版本明显更长,我认为表达力较差。有没有办法让它更短(理想情况下不引入魔术字符串)?
  2. input.Subscribe(x => Console.WriteLine(textBox.Text)); 不是很好,因为它引用了外部代码。我想通过“发件人”属性以某种方式访问​​它。

我的程序集来自 NuGet,它的版本是 1.0.10621.0。

4

1 回答 1

3

如果你想模仿旧的方式,你可以使用这个FromEventPattern方法:

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

这样做的唯一缺点是您失去了使用 FromEvent 方法时获得的类型安全性。(FromEventPattern也有一个使用委托的重载,但它处理EventHandler<T>MouseMove在.Net 2.0之前)

至于您的第二点,为什么“引用外部代码”是一件坏事?闭包是 C# 语言的重要组成部分,确实没有理由避免使用它们。实际上,在事件订阅代码中的代码中,您也在这样做(捕获表单):

var mouseMoves = Observable.FromEvent<MouseEventHandler, MouseEventArgs>(
               handlerAction => (_, args) => handlerAction(args),
               handler => form.MouseMove += handler,
               handler => form.MouseMove -= handler);
于 2012-07-06T17:48:43.143 回答