You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ReactiveUI中仅当Deleted属性从false变为true时触发SearchResults更新

Solution for Triggering SearchResults Only When Deleted Goes from False to True (Plus SearchTerm Changes)

Got it, let's fix this issue. The problem right now is that any change to SelectedItem.Deleted—including switching to a non-deleted item—triggers an unwanted refresh. We need to split our trigger sources into two parts: one for any SearchTerm change (keeping your original behavior), and another that only fires when Deleted transitions specifically from false to true.

Here's how to adjust your code:

// 1. Stream for any SearchTerm changes (preserves original behavior)
var searchTermUpdates = this.WhenAnyValue(x => x.SearchTerm);

// 2. Stream that only triggers when SelectedItem.Deleted goes from false → true
var deletedStateTransition = this.WhenAnyValue(x => x.SelectedItem)
    // Guard against null SelectedItem to avoid null reference exceptions
    .Where(selectedItem => selectedItem != null)
    // Switch to listening to the Deleted property of the currently selected item
    .Select(selectedItem => selectedItem.WhenAnyValue(x => x.Deleted))
    .Switch()
    // Track the previous and current value of Deleted
    .PairWithPrevious()
    // Only keep the cases where we went from false to true
    .Where(transition => !transition.Previous && transition.Current);

// Merge both streams: trigger search on SearchTerm change OR valid Deleted transition
_searchResults = searchTermUpdates
    .Merge(deletedStateTransition.Select(_ => this.SearchTerm))
    .Select(searchTerm => SearchProjects(searchTerm))
    .ObserveOn(RxApp.MainThreadScheduler)
    .ToProperty(this, x => x.SearchResults);

Breakdown of the changes:

  • searchTermUpdates: This keeps your original logic intact—any change to SearchTerm will immediately trigger a refresh of SearchResults.
  • deletedStateTransition:
    • We first filter out null SelectedItem to prevent crashes when no item is selected.
    • Switch() ensures we're always listening to the Deleted property of the currently selected item (not old, unselected items).
    • PairWithPrevious() is a ReactiveUI extension that captures both the previous and current value of Deleted. We use this to strictly filter for the false → true transition you care about.
  • Merging the streams: When the valid Deleted transition happens, we grab the current SearchTerm (since it hasn't changed) and pass it to SearchProjects, just like we do when SearchTerm updates.

If you don't have access to PairWithPrevious():

If for some reason you can't use that extension, you can replicate its behavior with Scan:

.Select(deleted => new { Current = deleted, Previous = (bool?)null })
.Scan((lastState, newState) => new { Current = newState.Current, Previous = lastState.Current })
.Where(state => state.Previous == false && state.Current == true)

This works exactly the same way, tracking the previous value of Deleted to filter only the transition you want.

Now, switching to a non-deleted item won't trigger an unwanted refresh—only when you mark an item as deleted (set Deleted from false to true) or change the SearchTerm will SearchResults update.

内容的提问来源于stack exchange,提问作者Hanabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:02:30