Reactive UI中有几种扩展方法,用于获取属性更改的可观察对象.
我想我明白WhenAny
和WhenAnyValue
. WhenAny
用于一系列属性更改通知,其中您希望哪个对象和属性的元数据发生了更改,而WhenAnyValue
当您真正想要更改值的流时.
首先,这是一个准确的评估吗?
怎么样WhenAnyDynamic
,WhenAnyObservable
和ObservableForProperty
?我无法弄清楚它们的用途,或者它们与前两个有何不同.它们都是供公众使用的吗?他们的目的是什么?
我想我理解WhenAny和WhenAnyValue.
让我通过代码演示:
// These two statements are 100% identical, but the latter looks nicer. this.WhenAny(x => x.Foo.Bar, x => x.Value) this.WhenAnyValue(x => x.Foo.Bar);
那么WhenAnyDynamic,WhenAnyObservable和ObservableForProperty怎么样?
WhenAnyDynamic
就像WhenAny,但是当你想要观察的东西不是常数时 - 你可能不需要它,但是RxUI内部会这样做.
WhenAnyObservable
让你获得一个Observable,但不必担心背后的物体变化.例如
this.SomeChildViewModel.MyCoolCommand .Subscribe(x => Console.WriteLine("Clicked!")); // Later... this.SomeChildViewModel = new SomeChildViewModel(); // (Hey, why doesn't my Clicked! handler show up anymore! I'm still subscribed // to the old object but it's super not obvious that's what happened)
与
this.WhenAnyObservable(x => x.MyCoolCommand). .Subscribe(x => Console.WriteLine("Clicked!")); // Later... this.SomeChildViewModel = new SomeChildViewModel(); // Cool, everything still works.
WhenAnyObservable在View to Subscribe to Commands中非常有用.
ObservableForProperty
就像WhenAny但最初订阅时不会触发.您可能应该忽略它,它实际上只是出于兼容性原因而出现的WhenAny的构建块.