English 中文(简体)
Reactive Framework for .NET examples that prove its usefulness [closed]
原标题:
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 8 years ago.

There s been quite some hype around the new Reactive Framework in .NET 4.0. While I think I undestood its basic concept I am not completely sold that it is that useful. Can you come up with a good example (that is halfway easy to understand) that totally shows of the power and usefullness of Rx? Show something that makes life so much easier when done with Rx.

最佳回答

Here is a quick example. Program a drag operation in a fully declarative manner, using LINQ to events.

   //Create an observable with the initial position and dragged points using LINQ to Events
   var mouseDragPoints = from md in e.GetMouseDown()
                           let startpos=md.EventArgs.GetPosition(e)
                           from mm in e.GetMouseMove().Until(e.GetMouseUp())
                           select new
                           {
                             StartPos = startpos,
                             CurrentPos = mm.EventArgs.GetPosition(e),
                           };

And draw a line from startpos to current pos

//Subscribe and draw a line from start position to current position  
            mouseDragPoints.Subscribe  
                (item =>  
                { 
                  //Draw a line from item.Startpos to item.CurrentPos
                }
                ); 

As you can see, there are no event handlers all over the places, nor boolean variables for managing the state.

If you are curious about those GetEventName() methods, suggesting you to read this entire article and download the source code and play with it.

Read it here and play with the source >>

问题回答

I wrote a demo step-by-step on my blog recently : http://blog.andrei.rinea.ro/2013/06/01/bing-it-on-reactive-extensions-story-code-and-slides/

I m basically building a small app in WPF with Rx and Bing Search:

enter image description here

The app will wait till you stop typing and then do an async search and present the results. If before the results came you issue another search it will automatically drop the existing search.

You can force a search (to skip the waiting time) by pressing ENTER or the "Go!" button and can stop an ongoing search by pressing the Clear button. There is a busy indicator and some error handling (in case the network goes down, for example).

Main topics covered :

  • Creating an observable from an event (TextChanged, Button.Click etc)
  • Async delegate (async search)
  • TakeUntil extension
  • DistinctUntilChanged extension (including custom Equals)
  • Merge extension
  • Throttle extension
  • ObserveOn extension (for UI thread sync-ing)

.. and more!





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签