Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create Observables from existing events. You can use Observable.FromEvent for this purpose. You may write a GetMouseDown event wrapper like this, as an extension method for a UI Element.</p> <pre><code> public static IObservable&lt;Event&lt;MouseButtonEventArgs&gt;&gt; GetMouseDown (this UIElement el) { var allevents = Observable.FromEvent&lt;MouseButtonEventHandler, MouseButtonEventArgs&gt; ( h =&gt; new MouseButtonEventHandler(h), h =&gt; el.MouseDown += h, h=&gt; el.MouseDown -= h ); return allevents; } </code></pre> <p>And later, you may 'handle' your event in a completely declarative manner. Like</p> <pre><code>//create an observable declaratively var mouseDowns=button.GetMouseDown(); //subcribe and do what ever you need mouseDowns.Subscribe(arg=&gt; MessageBox.Show(arg.ClickCount.ToString()) ); </code></pre> <p>And here is how to create a 'drag' observable, with the intial position and current position.</p> <pre><code> //Get 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 subscribe here to mouseDragPoints </code></pre> <p>Read this article <a href="http://amazedsaint.blogspot.com/2009/11/linq-to-events-more-on-net-reactive.html" rel="nofollow noreferrer">LINQ to Events - More on .NET Reactive Extensions</a> and play with the source code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload