7

ReactiveUIのようなビューモデルがあります。イベントを発生させるさまざまなタイプのプロパティがいくつかあり、発生NotifyPropertyChangedしたときに呼び出されるメソッドをサブスクライブしたいのですが、実際の値には興味がありません。

私の現在のコードは少し醜いです(不透明なtrue選択のため)。イベントが発生したときにただ気遣うという意図を示すこれを表現する方法はありますか?

    this.ObservableForProperty(m => m.PropertyOne)
        .Select(_ => true)
        .Merge(this.ObservableForProperty(m => m.PropertyTwo).Select(_ => true))
   .Subscribe(...)

私は約8つのプロパティをマージしているので、表示されているよりも醜いです。

4

2 に答える 2

17

これはReactiveUIのように見えるので、WhenAny演算子を使用するのはどうですか。

this.WhenAny(x => x.PropertyOne, x => x.PropertyTwo, (p1, p2) => Unit.Default)
    .Subscribe(x => /* ... */);

ただし、一般的に、任意のObservableを組み合わせる場合は、非拡張メソッドを使用してこれをもう少し明確に記述することもできます。

Observable.Merge(
    this.ObservableForProperty(x => x.PropertyOne).Select(_ => Unit.Default),
    this.ObservableForProperty(x => x.PropertyTwo).Select(_ => Unit.Default),
    this.ObservableForProperty(x => x.PropertyThree).Select(_ => Unit.Default)
).Subscribe(x => /* ... */);

また、ReactiveObjectのすべてのプロパティをサブスクライブしている場合は、次を使用することをお勧めします。

this.Changed.Subscribe(x => /* ... */);
于 2011-11-05T16:03:19.477 に答える
2

意図を明確にするための拡張メソッドにすることができます。

public static IObservable<bool> IgnoreValue<T>(this IObservable<T> source)
{
    return source.Select(_ => true);
}

...

this.ObservableForProperty(m => m.PropertyOne).IgnoreValue()
.Merge(this.ObservableForProperty(m => m.PropertyTwo).IgnoreValue())
.Subscribe(..);
于 2011-11-05T02:31:37.917 に答える