1

It has been suggested to me that I ought to use Rx to debounce input for an AutoCompleteBox where the suggestion list comes from a web service. Obviously I don't want to run a slow query for updates on every keystroke, but on the other hand you also don't want too long a lag after the user pauses.

I had done it like this

public partial class MainPage : PhoneApplicationPage
{
  Timer _timerName;

  public MainPage()
  {
    InitializeComponent();
    _timerName = new Timer(QueryForNameSuggestions);
  }

  void QueryForNameSuggestions(object state)
  {
    //
  }

  private void searchtermName_TextChanged(object sender, RoutedEventArgs e)
  {
    //Each keystroke resets the timer, 500ms after you stop typing it queries
    _timerName.Change(500, Timeout.Infinite);
  }

}

which is easy to understand and works nicely; every keystroke (re)starts a half second wait culminating in a query.

I really have no idea how to do the same thing with Rx, but I'm willing to entertain the possibility that it will be an improvement. Could someone show me how this would be done with Rx, and explain what's better about the Rx way?

Rx: How can I respond immediately, and throttle subsequent requests seems close but I don't see how you'd detect incoming keystrokes without polling the value of the AutoCompleteBox, which doesn't seem like an improvement to me.

4

1 回答 1

2

看一下这个演示:使用响应式扩展 (Rx) 治愈您的事件处理忧郁症

于 2013-11-03T12:09:48.270 回答