简体   繁体   English

如何将使用Rx Observable.FromEventPattern编写的用于事件处理程序的函数转换为纯.net事件处理程序(不带Rx)

[英]How to convert a function written using Rx Observable.FromEventPattern for event handlers to a pure .net event handler(without Rx)

I have a code snippet that someone has written using Rx in C# that uses Observable.FromEventPattern for event handling. 我有一个有人使用C#中的Rx编写的代码片段,该片段使用Observable.FromEventPattern进行事件处理。

Here's the existing code that I have which uses Rx 这是我使用Rx的现有代码

private void RegisterToDigitFocusEvents(int index, TextBox digit)
{
    Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>(
        h => digit.GotFocus += h,
        h => digit.GotFocus -= h)
        .SubscribeToElement(digit,
            _ => this.ResetDigit(digit, index),
            error => this.Log().Error("Observing digit GotFocus", error),
            () =>
            {
                /* Do nothing on complete */
            });
}

I am not allowed to use Rx in my project. 我不允许在我的项目中使用Rx。 I looked up the documentation for FromEventPattern, I was unable to clearly understand what is the pure .Net equivalent of writing the same code without Rx 我查阅了FromEventPattern的文档,无法清楚地了解在没有Rx的情况下编写相同代码的纯.Net等价方式。

Here's my attempted conversion which as you might know didn't work quite as expected 这是我尝试的转换,您可能知道它没有按预期工作

//Global index so that it can be used across functions
int index1;

private void RegisterToDigitFocusEvents(int index, TextBox digit)
{
    digit.GotFocus += digit_GotFocus;
    index1 = index;
}

void digit_GotFocus(object sender, RoutedEventArgs e)
{
    this.ResetDigit(sender as TextBox, index1);
}

As you notice I haven't written code to unregister the event handler. 如您所见,我尚未编写代码来注销事件处理程序。 I am not sure where to put the logic. 我不确定逻辑应该放在哪里。

I am a beginner to Rx. 我是Rx的初学者。 Just to be clear - I do not want re implement the whole observable pattern that Rx provides me, I just want to write a function equivalent of the first snippet without using Rx, pure .Net events. 只是要清楚一点-我不想重新实现Rx提供给我的整个可观察模式,我只想编写一个等效于第一个代码片段的函数,而不使用Rx和纯.Net事件。 Any help to write the first code snippet in this post without Rx would be highly appreciated. 我们非常感谢您在不使用Rx的情况下编写本文的第一个代码段的任何帮助。

It seems to me that this is the pure equivalent: 在我看来,这是完全等效的:

    private void RegisterToDigitFocusEvents(int index, TextBox digit)
    {
        RoutedEventHandler gotFocusHandler = (s, e) =>
        {
            try
            {
                this.ResetDigit(digit, index)
            }
            catch (Exception error)
            {
                this.Log().Error("Observing digit GotFocus", error);
            }
        };

        digit.GotFocus += gotFocusHandler;
    }

You really should extend this code to handle the detaching of the handler when your form closes, but the above should be the basics required. 您确实应该扩展此代码,以在窗体关闭时处理处理程序的分离,但是以上内容是必需的基础知识。

Please let me know if I've misunderstood the question. 如果我误解了这个问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何向Observable.FromEventPattern中使用的事件注册/注销处理程序? - How do you register/unregister handlers to the event used in Observable.FromEventPattern? 在 Reactive Extensions for .NET 中使用 Observable.FromEventPattern 时如何避免任何阻塞? - How can I avoid any blocking when using Observable.FromEventPattern in Reactive Extensions for .NET? 如何根据事件中的条件完成Rx Observable - How to complete a Rx Observable depending on a condition in a event 当使用TestScheduler将事件触发到具有ObserveOn的Observable.FromEventPattern时,在下一个事件被触发之前不会观察到事件 - When using TestScheduler to fire events into Observable.FromEventPattern that has an ObserveOn, the events aren't observed until next event is fired 在FromEventPattern Observer中使用Rx超时 - Using Rx Timeout with FromEventPattern Observer Observable.fromEventPattern TypedEventHandler - Observable.fromEventPattern TypedEventHandler ObservableCollection上的Observable.FromEventPattern - Observable.FromEventPattern on ObservableCollection 如何在不使用 Rx 框架的情况下限制事件的速度 - How to throttle the speed of an event without using Rx Framework 将基于事件的代码转换为Rx - Convert Event based code to Rx 如何使用RX限制事件流? - How to throttle event stream using RX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM