Two years later, Microsoft released the reactive extensions (RX) Library. RX combines the event-driven UI with LINQ, concurrency, and asynchronous calls.
RX tries to solve the problem of asynchronous data access from the event-based UI. The standard iterator mode and its basic interfaces ienumerable and ienumerator are insufficient for asynchronous operations. Therefore, RX introduces the observer mode to solve this problem. This mode includes two main interfaces, iobservable and iobserver. It is not the client that iterates the data set step by step, but the set pushes the data to the client as the result of asynchronous call, ending the call loop.
Next, we use the rx ui event subscription function to implement the function of dragging elements in WPF/Silverlight:
1 Private Void Enabledragging (frameworkelement element)
2 {
3 VaR Mousedown = From EVT In Observable. fromeventpattern <mousebuttoneventargs> (element, " Mousedown " )
4 Select EVT. eventargs. getposition ( This );
5
6 VaR Mouseup = From EVT In Observable. fromeventpattern <mousebuttoneventargs> (This , " Mouseup " )
7 Select EVT. eventargs. getposition ( This );
8
9 VaR Mousemove = From EVT In Observable. fromeventpattern <mouseeventargs> ( This ," Mousemove " )
10 Select EVT. eventargs. getposition ( This );
11
12 VaR Elementmoves = From Start In Mousedown
13 From PosIn Mousemove. startwith (start). takeuntil (mouseup)
14 Select Pos;
15
16 Elementmoves. subscribe (value => {
17 Canvas. setleft (element, value. X-element. width/ 2 );
18 Canvas. settop (element, value. Y-element. Height/ 2 );
19 });
20 }
Just a few linesCodeIt implements the functions we want to accomplish and is also very easy to understand in semantics.
Of course, this is only a small part of the RX framework. For more information, go to the official Developer Center.