Wednesday, October 27, 2010

Rx Lazy Loading Text Box

I started using .Net Reactive (Rx) extensions for work recently.
As a WPF UI developer, one of the nice way of handling a users input is not to do anything until the user have complete his/her action.

In this example; I have a text box, which will auto filter a data grid when the user stop typing in the text box and the text changed event is fired.
This will improve the UI responsiveness, instead of firing after every key is entered.

// Method OnSearchTextChanged is called .5s after Text Box text changed
Observable.FromEvent<TextChangedEventArgs>(textbox, "TextChanged")
.Throttle(TimeSpan.FromSeconds(.5))
.ObserveOnDispatcher()
.Subscribe(OnSearchTextChanged);

This was only possible in the past throught the use of Timers and Thread.Sleep, Rx makes this easier to do.

Lazy Loading Text Box

Please let me know if this helps you.

No comments: