From 2b84771d1fea8df64cb66916eb146ea8eb84dfec Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 11 Jul 2020 00:11:45 +0200 Subject: [PATCH] Added SetAndNotifyOnCompletion overload with callback --- .../ComponentModel/ObservableObject.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Microsoft.Toolkit.Mvvm/ComponentModel/ObservableObject.cs b/Microsoft.Toolkit.Mvvm/ComponentModel/ObservableObject.cs index 888a1900..d4795903 100644 --- a/Microsoft.Toolkit.Mvvm/ComponentModel/ObservableObject.cs +++ b/Microsoft.Toolkit.Mvvm/ComponentModel/ObservableObject.cs @@ -350,6 +350,36 @@ namespace Microsoft.Toolkit.Mvvm.ComponentModel return SetAndNotifyOnCompletion(ref field, fieldExpression, newValue, out _); } + /// + /// 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 + /// event, updates the field and then raises the event. + /// This method is just like , + /// with the difference being an extra parameter with a callback being invoked + /// either immediately, if the new task has already completed or is , or upon completion. + /// + /// The type of to set and monitor. + /// The field storing the property's value. + /// + /// An returning the field to update. + /// The property's value after the change occurred. + /// A callback to invoke to update the property value. + /// (optional) The name of the property that changed. + /// if the property was changed, otherwise. + /// + /// The and events are not raised + /// if the current and new value for the target property are the same. + /// + protected bool SetAndNotifyOnCompletion(ref TTask? field, Expression> fieldExpression, TTask? newValue, Action callback, [CallerMemberName] string propertyName = null!) + where TTask : Task + { + bool result = SetAndNotifyOnCompletion(ref field, fieldExpression, newValue, out Task task); + + task.ContinueWith(t => callback((TTask?)t)); + + return result; + } + /// /// Throws an when a given is invalid for a property. ///