我正在尝试使用反应式扩展动手实验室,但无法使用 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();
试试这个:
var subscription = Observable.FromEventPattern<TextChangedEventArgs>(txt, "TextChanged");
尝试:
var subscription = Observable.FromEvent(h => txt.TextChanged += h,
h => txt.TextChanged -= h);
试试这个(编辑:如果你的目标是Windows Phone Reactive API):
var subscription = Observable.FromEvent<TextChangedEventArgs>(txt, "TextChanged");