当用户在文本框中输入内容时,我必须ReactiveAsyncCommand
执行搜索,该文本框的设置如下:
var results = SearchCommand.RegisterAsyncFunction(term =>
PerformSearch((string)term));
this.ObservableForProperty(x => x.SearchTerm)
.Throttle(TimeSpan.FromMilliseconds(800))
.Select(x => x.Value).DistinctUntilChanged()
.Where(x => !String.IsNullOrWhiteSpace(x))
.InvokeCommand(SearchCommand);
_SearchResults = results.ToProperty(this, x => x.SearchResults);
问题是搜索功能可能会很慢,因为它需要执行数据库查询并显示过时的结果,我认为这是由于ReactiveAsyncCommand
在当前异步任务完成之前没有再次运行。
所以我的问题是,我怎样才能取消正在运行的异步任务并从当前搜索词重新开始,或者如果它不是当前搜索词,则完全删除结果。
这似乎与本讨论的第二部分相同,但我不确定如何将其应用于我的代码,因为我的搜索代码返回的是 IEnumerable 而不是 IObservable。
请注意 RxUI 4 的这一点,因为它是一个 .NET 4 应用程序。
更新:PerformSearch 方法
private List<WizardLocationSearchResult> PerformSearch(string searchTerm)
{
var results = new List<WizardLocationSearchResult>();
bool isMatch = false;
if (Regex.IsMatch(searchTerm, _postcodeRegex, RegexOptions.IgnoreCase))
{
var locationResult = _locationService.GetByPostcode(searchTerm);
_locationService.DeepLoad(locationResult, true, Data.DeepLoadType.IncludeChildren, typeof(TList<EnterpriseAndHolding>));
results.AddRange(ProcessLocationSearches(locationResult));
isMatch = true;
}
if (!isMatch)
{
var query = new LocationParameterBuilder(true, false);
string formattedSearchTerm = searchTerm + "%";
query.AppendLike(LocationColumn.Address1, formattedSearchTerm);
query.AppendLike(LocationColumn.Address2, formattedSearchTerm);
query.AppendLike(LocationColumn.Town, formattedSearchTerm);
query.AppendLike(LocationColumn.PostalTown, formattedSearchTerm);
query.AppendLike(LocationColumn.County, formattedSearchTerm);
var locationResult = _locationService.Find(query.GetParameters());
_locationService.DeepLoad(locationResult, true, Data.DeepLoadType.IncludeChildren, typeof(TList<EnterpriseAndHolding>));
results.AddRange(ProcessLocationSearches(locationResult));
}
return results;
}