Here is what I am trying to achieve (in a .Net 4 WPF client):
- User types some text in a text box
- After half a second of no more typing, text is sent asynchronously sent to a server for validation
- Validation result is shown to the user (this is just text bound to a text block)
- If the user types more text while the validation is in progress (this takes a couple of seconds) the results from server are ignored and a new validation request is sent instead
From what I have read reactive extensions are a good fit for this but I’m having trouble, in particular step 4.
Secondly I am using mvvm, so how do I put this logic in the view model while subscribing to TextBox.TextChanged in the view.
A few operators come in quite handy here (notably,
Throttlefor 2 andSwitchfor 4). Your view model will look something like:From the view, bind the text box to Input and set the binding mode to PropertyChanged (instead of the default LostFocus). The result block can then bind to Output.