gpt4 book ai didi

c# - 响应式(Reactive) UI/响应式(Reactive)扩展 : how to clear ObservableAsPropertyHelper

转载 作者:行者123 更新时间:2023-11-30 17:01:09 27 4
gpt4 key购买 nike

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

我已经设置了 ObservableAsPropertyHelper,它是 ViewModel 中 SearchTerm 属性的建议机场的输出。以下是 ObservableAsPropertyHelper 的定义。在 View 中,我有一个绑定(bind)到此属性的列表框。我希望能够明确清除列表框(因为一旦用户选择了建议的项目,我想用所选机场填充 SearchTerm 并清除建议列表)。有没有一种优雅的方式来实现它?

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);

最佳答案

我会这样做 - 这有点棘手,因为实际的事件序列会反馈到自身(即选择一个项目集 SearchTerms)

// 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);

关于c# - 响应式(Reactive) UI/响应式(Reactive)扩展 : how to clear ObservableAsPropertyHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21349363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com