RX AutoCompleteBox

浪尽此生 提交于 2019-12-06 04:35:18

问题


I'm trying to build an filter control using RX and WPF. So I have a textbox and a listbox. On start up the listbox has 100 contact names and the user can type in a name to filter the list.

Question is how can I build up an text stream (key inputs) and then publish. This should be Time sensitive so I guess only after 750milliseconds if a key input hasnt been detected then the filter may be performed.

Thanks


回答1:


The basic outline would looks like so

  1. textbox keydown event converted to an IO
  2. throttling of the keystrokes, so that we don't search while the user is actually typing
  3. Do the search
  4. Place the the search results onto the list box

Here's some pseudo-code -

 var keysIO =   Observable.FromEvent<KeyDownEventHandler, RoutedEventArgs>(
                                    h => new KeyDownEventHandler(h),
                                    h => btn.KeyDown += h,
                                    h => btn.KeyDown -= h));

 var searchResults = keysIO.Throttle(TimeSpan.FromSeconds(0.750),Scheduler.Dispatcher);

 searchResults.Subscribe(sr => {  lb.Clear(); lb.AddRange(sr); });

@Andy, Throttle won't kick off a search every 750ms, only after the user has stopped typing for 750ms. Try this in LinqPad.

   Observable.Interval(TimeSpan.FromMilliseconds(10))
   .Do(ii =>  "keystroke".Dump())
   .Take(10)
   .Throttle(TimeSpan.FromSeconds(0.750))
   .Select(ttl => "search")



回答2:


What Scott Weinstein is suggesting is correct.

Additionally, since you want to affect a Gui control, you have to make sure to either ObserveOn the Dispatcher or use the scheduler somewhere before you subscribe, to get you back to the dispatcher thread.

This worked for me:

 Observable.FromEvent<TextChangedEventArgs>(TextBox, "TextChanged")
                .Throttle(TimeSpan.FromSeconds(0.75), Scheduler.Dispatcher)
                .Select(obs => TextBox.Text)
                .Subscribe(TextChangedTo);

Now in the TextChangedTo(text) method you would populate your list with contact names.




回答3:


On new versions of Rx the Scheduler.Dispatcher has disappeared and FromEvent seems that does not work well with WPF, so, for anyone that needs a solution today here you have a working solution for a textbox named FilterText:

Observable.FromEventPattern<TextChangedEventHandler, TextChangedEventArgs>(
    h => this.FilterText.TextChanged += h,
    h => this.FilterText.TextChanged -= h)
    .Throttle(new TimeSpan(0, 0, 0, 0, 750))
    .ObserveOnDispatcher()
    .Subscribe(t => DoFiltering(this.FilterText.Text));



回答4:


There's a full example here, with slides and source code



来源:https://stackoverflow.com/questions/5438639/rx-autocompletebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!