What are the distinctions between the various WhenAny methods in Reactive UI

∥☆過路亽.° 提交于 2019-11-29 05:18:07

问题


There are several extension methods in Reactive UI for getting observables for property changes.

I think I understand WhenAny and WhenAnyValue. WhenAny is for a series of property change notifications where you want the metadata of which object and property had the change, while WhenAnyValue is for when you really just want the stream of changed values.

First of all, is that an accurate assessment?

What about WhenAnyDynamic, WhenAnyObservable, and ObservableForProperty? I can't really figure out what they're for, or how they're distinct from the first two. Are they all intended for public use? What is their purpose?


回答1:


I think I understand WhenAny and WhenAnyValue.

Let me demonstrate via code:

// 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);

What about WhenAnyDynamic, WhenAnyObservable, and ObservableForProperty?

WhenAnyDynamic is like WhenAny but when the things you want to observe aren't constants - you probably won't need it, but RxUI internals does.

WhenAnyObservable lets you get an Observable, but not have to worry about objects changing behind your back. For example

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)

Versus

this.WhenAnyObservable(x => x.MyCoolCommand).
    .Subscribe(x => Console.WriteLine("Clicked!"));

// Later...
this.SomeChildViewModel = new SomeChildViewModel();

// Cool, everything still works.

WhenAnyObservable is super useful in the View to Subscribe to Commands.

ObservableForProperty is like WhenAny but doesn't fire when initially subscribed to. You probably should ignore it, it's really just a building block for WhenAny that is around for compatibility reasons.



来源:https://stackoverflow.com/questions/22213444/what-are-the-distinctions-between-the-various-whenany-methods-in-reactive-ui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!