【问题标题】:ReactiveUI CombineLatest not trigger FuncReactiveUI CombineLatest 不触发 Func
【发布时间】:2020-01-24 05:33:40
【问题描述】:

我在 WPF 中使用 ReactiveUI。我将一个ListBox.ItemSource 绑定到我的ViewModel.FileList

下面的Observable.CombinedLatest() 不会触发 Func。 但是上面的单个 Observable 触发了它的 Func。

我想使用组合 observable 的原因是: 我想在第一次加载时显示数据列表 - 这是由 ConvertedMessage 属性值更改触发的。

然后当用户在搜索文本框中输入单词时,我还需要显示不同的列表,它们实际上都更新了相同的 DataBinding 源列表。

我还尝试了两个不同的单独的 observable,Observable.SelectMany(MyMethod) 中的两个单独的方法,但似乎第二个 observable 覆盖了前一个 - 因为只有最后一个 observable.MyMethod 被调用。

关于 ReactiveUI CombinedLatest() 有什么想法或建议吗,它是否仅在所有可观察对象产生元素时才触发该功能?

this.WhenAnyValue(x => x.ConvertedMessage).Where(x=>x!=null).Subscribe(x => { var msg = x.Message; });

        var initial =
            this                
            .WhenAnyValue(x => x.ConvertedMessage)
            .Where(x=>x!=null);

        var searchObjs =
            this
            .WhenAnyValue(x => x.SearchText)
            .Throttle(TimeSpan.FromMilliseconds(800))
            .Select(term => term)
            .DistinctUntilChanged()
            .Where(term => !string.IsNullOrEmpty(term));

        reviewFiles = Observable.CombineLatest(initial, searchObjs, (message, term) => {
            ObservableCollection<PrecedentFile> loadedFiles = new ObservableCollection<PrecedentFile>();

            var start = 0;
            CurrentPage = 1;
            if (!string.IsNullOrEmpty(term))
            {
                //some logic
                return searchedResults;
            }
            else
            {
                //other logic

                return loadedFiles.AsEnumerable<PrecedentFile>();
            }

        }).Select(x=>x)
            .ObserveOn(RxApp.MainThreadScheduler)
            .ToProperty(this, x => x.ReviewFiles);

【问题讨论】:

    标签: wpf observable reactiveui


    【解决方案1】:

    我相信您面临的问题是 CombineLatest 需要传递给它的所有可观察对象至少至少发生一次事件/信号。

    在上面的示例中,如果initial 有触发器但searchObs 没有,则CombineLatest 不会触发。

    例如,您已将 ConvertedMessage 更改了 3 次,但 SearchText 从未更改过,那么 CombineLatest 将不会触发。

    您可以做的一件事是删除 .Where(x =&gt; x != null)Where(term =&gt; !string.IsNullOrEmpty(term)) 语句并在您的 CombineLatest lambda 中处理这些条件。 WhenAnyValue() 将是默认信号,带有您的属性的初始值(通常为空)。这样双方就已经有了价值信号。

    【讨论】:

    • 对于可能不会立即触发的 observable,您可以做的另一件事是使用 .StartsWith("") 来确保 combine latest to pick 的值。
    • 感谢 Glenn,您的建议很有帮助,当我删除两个 where 文件管理器并在下面的代码中添加条件处理程序时,它确实在任一来源上触发。
    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 2018-05-13
    • 1970-01-01
    • 2018-11-01
    相关资源
    最近更新 更多