Class ObservableDispatcherObject
Represents a dispatcher object with methods for observable properties support.
[ObservableObject]
public abstract class ObservableDispatcherObject : DispatcherObject, INotifyPropertyChanged, INotifyPropertyChanging
- Inheritance
-
ObservableDispatcherObject
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
Note that this class shouldn't used directly because it doesn't contain any logic. If you want to generate properties with dispatcher checks, use BindableObject instead.
Methods
OnPropertyChanged(PropertyChangedEventArgs)
Raises the PropertyChanged event.
[ExcludeFromCodeCoverage]
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
Parameters
ePropertyChangedEventArgsThe input PropertyChangedEventArgs instance.
OnPropertyChanged(string?)
Raises the PropertyChanged event.
[ExcludeFromCodeCoverage]
protected void OnPropertyChanged(string? propertyName = null)
Parameters
propertyNamestring(optional) The name of the property that changed.
OnPropertyChanging(PropertyChangingEventArgs)
Raises the PropertyChanging event.
[ExcludeFromCodeCoverage]
protected virtual void OnPropertyChanging(PropertyChangingEventArgs e)
Parameters
ePropertyChangingEventArgsThe input PropertyChangingEventArgs instance.
OnPropertyChanging(string?)
Raises the PropertyChanging event.
[ExcludeFromCodeCoverage]
protected void OnPropertyChanging(string? propertyName = null)
Parameters
propertyNamestring(optional) The name of the property that changed.
SetPropertyAndNotifyOnCompletion(ref TaskNotifier?, Task?, Action<Task?>, string?)
Compares the current and new values for a given field (which should be the backing field for a property). If the value has changed, raises the PropertyChanging event, updates the field and then raises the PropertyChanged event. This method is just like SetPropertyAndNotifyOnCompletion(ref TaskNotifier?, Task?, string?), with the difference being an extra Action<T> parameter with a callback being invoked either immediately, if the new task has already completed or is null, or upon completion.
[ExcludeFromCodeCoverage]
protected bool SetPropertyAndNotifyOnCompletion(ref ObservableDispatcherObject.TaskNotifier? taskNotifier, Task? newValue, Action<Task?> callback, string? propertyName = null)
Parameters
taskNotifierObservableDispatcherObject.TaskNotifierThe field notifier to modify.
newValueTaskThe property's value after the change occurred.
callbackAction<Task>A callback to invoke to update the property value.
propertyNamestring(optional) The name of the property that changed.
Returns
Remarks
The PropertyChanging and PropertyChanged events are not raised if the current and new value for the target property are the same.
SetPropertyAndNotifyOnCompletion(ref TaskNotifier?, Task?, string?)
Compares the current and new values for a given field (which should be the backing field for a property). If the value has changed, raises the PropertyChanging event, updates the field and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<T>(ref T, T, string?), with the difference being that this method will also monitor the new value of the property (a generic Task) and will also raise the PropertyChanged again for the target property when it completes. This can be used to update bindings observing that Task or any of its properties. This method and its overload specifically rely on the ObservableDispatcherObject.TaskNotifier type, which needs to be used in the backing field for the target Task property. The field doesn't need to be initialized, as this method will take care of doing that automatically. The ObservableDispatcherObject.TaskNotifier type also includes an implicit operator, so it can be assigned to any Task instance directly. Here is a sample property declaration using this method:
private TaskNotifier myTask;
public Task MyTask
{
get => myTask;
private set => SetAndNotifyOnCompletion(ref myTask, value);
}
[ExcludeFromCodeCoverage]
protected bool SetPropertyAndNotifyOnCompletion(ref ObservableDispatcherObject.TaskNotifier? taskNotifier, Task? newValue, string? propertyName = null)
Parameters
taskNotifierObservableDispatcherObject.TaskNotifierThe field notifier to modify.
newValueTaskThe property's value after the change occurred.
propertyNamestring(optional) The name of the property that changed.
Returns
Remarks
The PropertyChanging and PropertyChanged events are not raised if the current
and new value for the target property are the same. The return value being true only
indicates that the new value being assigned to taskNotifier is different than the previous one,
and it does not mean the new Task instance passed as argument is in any particular state.
SetPropertyAndNotifyOnCompletion<T>(ref TaskNotifier<T>?, Task<T>?, Action<Task<T>?>, string?)
Compares the current and new values for a given field (which should be the backing field for a property). If the value has changed, raises the PropertyChanging event, updates the field and then raises the PropertyChanged event. This method is just like SetPropertyAndNotifyOnCompletion<T>(ref TaskNotifier<T>?, Task<T>?, string?), with the difference being an extra Action<T> parameter with a callback being invoked either immediately, if the new task has already completed or is null, or upon completion.
[ExcludeFromCodeCoverage]
protected bool SetPropertyAndNotifyOnCompletion<T>(ref ObservableDispatcherObject.TaskNotifier<T>? taskNotifier, Task<T>? newValue, Action<Task<T>?> callback, string? propertyName = null)
Parameters
taskNotifierObservableDispatcherObject.TaskNotifier<T>The field notifier to modify.
newValueTask<T>The property's value after the change occurred.
callbackAction<Task<T>>A callback to invoke to update the property value.
propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe type of result for the Task<TResult> to set and monitor.
Remarks
The PropertyChanging and PropertyChanged events are not raised if the current and new value for the target property are the same.
SetPropertyAndNotifyOnCompletion<T>(ref TaskNotifier<T>?, Task<T>?, string?)
Compares the current and new values for a given field (which should be the backing field for a property). If the value has changed, raises the PropertyChanging event, updates the field and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<T>(ref T, T, string?), with the difference being that this method will also monitor the new value of the property (a generic Task) and will also raise the PropertyChanged again for the target property when it completes. This can be used to update bindings observing that Task or any of its properties. This method and its overload specifically rely on the ObservableDispatcherObject.TaskNotifier<T> type, which needs to be used in the backing field for the target Task property. The field doesn't need to be initialized, as this method will take care of doing that automatically. The ObservableDispatcherObject.TaskNotifier<T> type also includes an implicit operator, so it can be assigned to any Task instance directly. Here is a sample property declaration using this method:
private TaskNotifier<int> myTask;
public Task<int> MyTask
{
get => myTask;
private set => SetAndNotifyOnCompletion(ref myTask, value);
}
[ExcludeFromCodeCoverage]
protected bool SetPropertyAndNotifyOnCompletion<T>(ref ObservableDispatcherObject.TaskNotifier<T>? taskNotifier, Task<T>? newValue, string? propertyName = null)
Parameters
taskNotifierObservableDispatcherObject.TaskNotifier<T>The field notifier to modify.
newValueTask<T>The property's value after the change occurred.
propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe type of result for the Task<TResult> to set and monitor.
Remarks
The PropertyChanging and PropertyChanged events are not raised if the current
and new value for the target property are the same. The return value being true only
indicates that the new value being assigned to taskNotifier is different than the previous one,
and it does not mean the new Task<TResult> instance passed as argument is in any particular state.
SetProperty<T>(T, T, Action<T>, string?)
Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. This overload is much less efficient than SetProperty<T>(ref T, T, string?) and it should only be used when the former is not viable (eg. when the target property being updated does not directly expose a backing field that can be passed by reference). For performance reasons, it is recommended to use a stateful callback if possible through the SetProperty<TModel, T>(T, T, TModel, Action<TModel, T>, string?) whenever possible instead of this overload, as that will allow the C# compiler to cache the input callback and reduce the memory allocations. More info on that overload are available in the related XML docs. This overload is here for completeness and in cases where that is not applicable.
[ExcludeFromCodeCoverage]
protected bool SetProperty<T>(T oldValue, T newValue, Action<T> callback, string? propertyName = null)
Parameters
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
callbackAction<T>A callback to invoke to update the property value.
propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe type of the property that changed.
Remarks
The PropertyChanging and PropertyChanged events are not raised if the current and new value for the target property are the same.
SetProperty<T>(T, T, IEqualityComparer<T>, Action<T>, string?)
Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. See additional notes about this overload in SetProperty<T>(T, T, Action<T>, string?).
[ExcludeFromCodeCoverage]
protected bool SetProperty<T>(T oldValue, T newValue, IEqualityComparer<T> comparer, Action<T> callback, string? propertyName = null)
Parameters
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
callbackAction<T>A callback to invoke to update the property value.
propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe type of the property that changed.
SetProperty<T>(ref T, T, IEqualityComparer<T>, string?)
Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. See additional notes about this overload in SetProperty<T>(ref T, T, string?).
[ExcludeFromCodeCoverage]
protected bool SetProperty<T>(ref T field, T newValue, IEqualityComparer<T> comparer, string? propertyName = null)
Parameters
fieldTThe field storing the property's value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe type of the property that changed.
SetProperty<T>(ref T, T, string?)
Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event.
[ExcludeFromCodeCoverage]
protected bool SetProperty<T>(ref T field, T newValue, string? propertyName = null)
Parameters
fieldTThe field storing the property's value.
newValueTThe property's value after the change occurred.
propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe type of the property that changed.
Remarks
The PropertyChanging and PropertyChanged events are not raised if the current and new value for the target property are the same.
SetProperty<TModel, T>(T, T, IEqualityComparer<T>, TModel, Action<TModel, T>, string?)
Compares the current and new values for a given nested property. If the value has changed, raises the PropertyChanging event, updates the property and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<T>(ref T, T, string?), with the difference being that this method is used to relay properties from a wrapped model in the current instance. See additional notes about this overload in SetProperty<TModel, T>(T, T, TModel, Action<TModel, T>, string?).
[ExcludeFromCodeCoverage]
protected bool SetProperty<TModel, T>(T oldValue, T newValue, IEqualityComparer<T> comparer, TModel model, Action<TModel, T> callback, string? propertyName = null) where TModel : class
Parameters
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
modelTModelThe model containing the property being updated.
callbackAction<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TModelThe type of model whose property (or field) to set.
TThe type of property (or field) to set.
SetProperty<TModel, T>(T, T, TModel, Action<TModel, T>, string?)
Compares the current and new values for a given nested property. If the value has changed, raises the PropertyChanging event, updates the property and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<T>(ref T, T, string?), with the difference being that this method is used to relay properties from a wrapped model in the current instance. This type is useful when creating wrapping, bindable objects that operate over models that lack support for notification (eg. for CRUD operations). Suppose we have this model (eg. for a database row in a table):
public class Person
{
public string Name { get; set; }
}
We can then use a property to wrap instances of this type into our observable model (which supports notifications), injecting the notification to the properties of that model, like so:
[ObservableObject]
public class BindablePerson
{
public Model { get; }
public BindablePerson(Person model)
{
Model = model;
}
public string Name
{
get => Model.Name;
set => Set(Model.Name, value, Model, (model, name) => model.Name = name);
}
}
This way we can then use the wrapping object in our application, and all those "proxy" properties will also raise notifications when changed. Note that this method is not meant to be a replacement for SetProperty<T>(ref T, T, string?), and it should only be used when relaying properties to a model that doesn't support notifications, and only if you can't implement notifications to that model directly (eg. by having it inherit from ObservableObject). The syntax relies on passing the target model and a stateless callback to allow the C# compiler to cache the function, which results in much better performance and no memory usage.
[ExcludeFromCodeCoverage]
protected bool SetProperty<TModel, T>(T oldValue, T newValue, TModel model, Action<TModel, T> callback, string? propertyName = null) where TModel : class
Parameters
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
modelTModelThe model containing the property being updated.
callbackAction<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TModelThe type of model whose property (or field) to set.
TThe type of property (or field) to set.
Remarks
The PropertyChanging and PropertyChanged events are not raised if the current and new value for the target property are the same.
Events
PropertyChanged
Occurs when a property value changes.
[ExcludeFromCodeCoverage]
public event PropertyChangedEventHandler? PropertyChanged
Event Type
PropertyChanging
Occurs when a property value is changing.
[ExcludeFromCodeCoverage]
public event PropertyChangingEventHandler? PropertyChanging