ReactiveUI/反应性扩展:如何清除ObservableAberPopertyHelper



我正在使用ReactiveUI框架来搜索世界上机场列表。

我已经设置了ObservableSpropertyHelper,这是ViewModel中的搜索属性属性所建议的机场的输出。以下是观测值的定义。在视图中,我有一个绑定到此属性的列表框。我希望能够明确清除列表箱(因为一旦用户选择了建议的项目,我想用所选机场填充搜索术语并清除建议的列表)。是否有一种优雅的方式来实施此方法?

var searchTerms = this.ObservableForProperty(x => x.SearchTerms).Where(x => canSearch).Value().Throttle(TimeSpan.FromMilliseconds(500));
var searchResults = searchTerms.SelectMany(SearchAirports);
var latestResults = searchTerms.CombineLatest(searchResults, (s, r) => r.SearchTerm != s ? null : r.AirportLiteWithWeights).Where(x => x != null);
_airportLiteWithWeights = latestResults.ToProperty(this, x => x.AirportLiteWithWeights);

这是我的方式 - 这有点棘手,因为事件的实际顺序会反馈自身(即选择一个项目集搜索器)

// The act of selecting an item is very ICommand'y, let's model it
// as such.
ReactiveCommand SuggestionItemSelected = new ReactiveCommand();
// Important to *not* signal a change here by touching the field
// so that we avoid a circular event chain
SuggestionItemSelected.Subscribe(x => _searchTerms = x);
// NB: Always provide a scheduler to Throttle!
var searchTerms = this.WhenAnyValue(x => x.SearchTerms)
    .Where(x => canSearch)
    .Throttle(TimeSpan.FromMilliseconds(500), RxApp.MainThreadScheduler);
// Select + Switch will do what you were trying to do with CombineLatest
var latestResults = searchTerms
    .Select(SearchAirports);
    .Switch()
// The listbox is the combination of *Either* the latest results, or the
// empty list if someone chooses an item
var latestResultsOrCleared = Observable.Merge(
    latestResults,
    SuggestionItemSelected.Select(_ => new List<Results>()));
latestResultsOrCleared
    .ToProperty(this, x => x.AirportLiteWithWeights, out _airportLiteWithWeights);

相关内容

  • 没有找到相关文章