Using RX to handle events
Have a look at the SetGestureDetectorListener
function where we are using the FromEventPattern
method from the Observable
framework. The function must be typed with a particular object (that is, MotionEvent
) that contains an EventHandler
property, in this case Touch
. Every time a Touch
event is fired, using the Window
method, we wait 0.7 seconds before doing anything (this ensures that we only respond to the first event taken every period set in the Window
method). Once this period is reached, SelectMany
is called and the first Touch
event is retrieved from the observable sequence via the Take
method. Then we call Subscribe
to assign the NotifyFocus
method, and pass in the MotionEvent
object taken from the SelectMany
method.
To summarize, the FromEventPattern
method is very useful for controlling multiple events and responding with specific actions. We have applied this technique with touch events because we want to make sure only one touch event is processed every...