Class ObservableAsPropertyHelper<T>
- Namespace
- ReactiveUI
- Assembly
- ReactiveUI.dll
ObservableAsPropertyHelper is a class to help ViewModels implement
"output properties", that is, a property that is backed by an
Observable. The property will be read-only, but will still fire change
notifications. This class can be created directly, but is more often created
via the OAPHCreation
public sealed class ObservableAsPropertyHelper<T> : IHandleObservableErrors, IDisposable, IEnableLogger
Type Parameters
TThe type.
- Inheritance
-
ObservableAsPropertyHelper<T>
- Implements
-
IEnable
Logger
- Extension Methods
Examples
_fullName = this.WhenAnyValue(x => x.FirstName, x => x.LastName,
(first, last) => $"{first} {last}")
.ToProperty(this, x => x.FullName);
public string FullName => _fullName.Value;
Remarks
Use this helper when the value for a property is derived from one or more observable streams (for example,
WhenAnyValue). The helper subscribes to the source observable, tracks the latest value, and raises change
notifications through the supplied callbacks.
Constructors
ObservableAsPropertyHelper(IObservable<T?>, Action<T?>, Action<T?>?, Func<T?>?, bool, IScheduler?)
Initializes a new instance of the ObservableAsPropertyHelper<T> class.
[RequiresDynamicCode("ObservableAsPropertyHelper uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("ObservableAsPropertyHelper uses methods that may require unreferenced code")]
public ObservableAsPropertyHelper(IObservable<T?> observable, Action<T?> onChanged, Action<T?>? onChanging = null, Func<T?>? getInitialValue = null, bool deferSubscription = false, IScheduler? scheduler = null)
Parameters
observableIObservable<T>The Observable to base the property on.
onChangedAction<T>The action to take when the property changes, typically this will call the ViewModel's RaisePropertyChanged method.
onChangingAction<T>The action to take when the property changes, typically this will call the ViewModel's RaisePropertyChanging method.
getInitialValueFunc<T>The function used to retrieve the initial value of the property.
deferSubscriptionboolA value indicating whether the ObservableAsPropertyHelper<T> should defer the subscription to the
observablesource until the first call to Value, or if it should immediately subscribe to theobservablesource.schedulerISchedulerThe scheduler that the notifications will provided on - this should normally be a Dispatcher-based scheduler.
ObservableAsPropertyHelper(IObservable<T?>, Action<T?>, Action<T?>?, T?, bool, IScheduler?)
Initializes a new instance of the ObservableAsPropertyHelper<T> class.
[RequiresDynamicCode("ObservableAsPropertyHelper uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("ObservableAsPropertyHelper uses methods that may require unreferenced code")]
public ObservableAsPropertyHelper(IObservable<T?> observable, Action<T?> onChanged, Action<T?>? onChanging = null, T? initialValue = default, bool deferSubscription = false, IScheduler? scheduler = null)
Parameters
observableIObservable<T>The Observable to base the property on.
onChangedAction<T>The action to take when the property changes, typically this will call the ViewModel's RaisePropertyChanged method.
onChangingAction<T>The action to take when the property changes, typically this will call the ViewModel's RaisePropertyChanging method.
initialValueTThe initial value of the property.
deferSubscriptionboolA value indicating whether the ObservableAsPropertyHelper<T> should defer the subscription to the
observablesource until the first call to Value, or if it should immediately subscribe to theobservablesource.schedulerISchedulerThe scheduler that the notifications will provided on - this should normally be a Dispatcher-based scheduler.
ObservableAsPropertyHelper(IObservable<T?>, Action<T?>, T?, bool, IScheduler?)
Initializes a new instance of the ObservableAsPropertyHelper<T> class.
[RequiresDynamicCode("ObservableAsPropertyHelper uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("ObservableAsPropertyHelper uses methods that may require unreferenced code")]
public ObservableAsPropertyHelper(IObservable<T?> observable, Action<T?> onChanged, T? initialValue = default, bool deferSubscription = false, IScheduler? scheduler = null)
Parameters
observableIObservable<T>The Observable to base the property on.
onChangedAction<T>The action to take when the property changes, typically this will call the ViewModel's RaisePropertyChanged method.
initialValueTThe initial value of the property.
deferSubscriptionboolA value indicating whether the ObservableAsPropertyHelper<T> should defer the subscription to the
observablesource until the first call to Value, or if it should immediately subscribe to theobservablesource.schedulerISchedulerThe scheduler that the notifications will be provided on - this should normally be a Dispatcher-based scheduler.
Properties
IsSubscribed
Gets a value indicating whether the ObservableAsPropertyHelper has subscribed to the source Observable. Useful for scenarios where you use deferred subscription and want to know if the ObservableAsPropertyHelper Value has been accessed yet.
Property Value
ThrownExceptions
Gets an observable which signals whenever an exception would normally terminate ReactiveUI internal state.
Property Value
Value
Gets the last provided value from the Observable.
Property Value
- T
Methods
Default(T?, IScheduler?)
Constructs a "default" ObservableAsPropertyHelper object. This is useful for when you will initialize the OAPH later, but don't want bindings to access a null OAPH at start up.
[RequiresDynamicCode("ObservableAsPropertyHelper uses methods that require dynamic code generation")]
[RequiresUnreferencedCode("ObservableAsPropertyHelper uses methods that may require unreferenced code")]
public static ObservableAsPropertyHelper<T> Default(T? initialValue = default, IScheduler? scheduler = null)
Parameters
initialValueTThe initial (and only) value of the property.
schedulerISchedulerThe scheduler that the notifications will be provided on - this should normally be a Dispatcher-based scheduler.
Returns
- Observable
AsProperty <T>Helper A default property helper.
Dispose()
Disposes this ObservableAsPropertyHelper.