ReactiveUI如何正确使用WhenAnyObservable

时间:2022-07-15 20:59:49

I am attempting to use WhenAnyObservable for the first time.

我试图第一次使用WhenAnyObservable。

When a ReactiveList Count == 0 and a tipText length is > 0 then I want to set a local value to true in the subscribe, or the opposite.

当ReactiveList Count == 0且tipText长度> 0时,我想在订阅中将本地值设置为true,或者相反。

        this.ViewModel.WhenAnyObservable(
            x => x.AutoCompleteItems.CountChanged,
            x => x.ObservableForProperty(y => y.TipText),
            (countChanged, tipText) => countChanged == 0 && tipText.Length > 0);

I am having trouble getting this to work.

我无法让这个工作。

Is there any trick I should be doing, or should I be using one of the other WhenAny commands?

有什么技巧我应该做,或者我应该使用其他一个WhenAny命令?

2 个解决方案

#1


4  

You've got the right idea, but WhenAnyObservable doesn't return items until it has an initial item for both "sides" if you use >1 Observables. So you probably want:

你有正确的想法,但是如果你使用> 1 Observables,则WhenAnyObservable不会返回项目,直到它有两个“边”的初始项。所以你可能想要:

this.ViewModel.WhenAnyObservable(
    x => x.AutoCompleteItems.CountChanged.StartWith(0),
    x => x.WhenAnyValue(y => y.TipText),
    (countChanged, tipText) => countChanged == 0 && tipText.Length > 0);

#2


1  

I get an index error when trying to use WhenAnyObservable. I ended up using

尝试使用WhenAnyObservable时出现索引错误。我最终使用了

Observable.CombineLatest(
    SomeItems.Changed.Select(x => true),
    this.WhenAnyValue(y => y.SomeBoolProperty),
    (b,g) => b && g)

#1


4  

You've got the right idea, but WhenAnyObservable doesn't return items until it has an initial item for both "sides" if you use >1 Observables. So you probably want:

你有正确的想法,但是如果你使用> 1 Observables,则WhenAnyObservable不会返回项目,直到它有两个“边”的初始项。所以你可能想要:

this.ViewModel.WhenAnyObservable(
    x => x.AutoCompleteItems.CountChanged.StartWith(0),
    x => x.WhenAnyValue(y => y.TipText),
    (countChanged, tipText) => countChanged == 0 && tipText.Length > 0);

#2


1  

I get an index error when trying to use WhenAnyObservable. I ended up using

尝试使用WhenAnyObservable时出现索引错误。我最终使用了

Observable.CombineLatest(
    SomeItems.Changed.Select(x => true),
    this.WhenAnyValue(y => y.SomeBoolProperty),
    (b,g) => b && g)