Using RX framework to drag objects in XAML

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.