/// <summary> /// Notifies listerners the calling property is being changed and modifies and excecutes given action. /// /// <para>Should be called in all bindable properties. Only notifies listeners when the property action was successful, /// unless otherwise stated using the <paramref name="overrideNotification"/> parameter.</para> /// </summary> /// <typeparam name="T">The property type.</typeparam> /// <param name="setPropertyAction">Action to be taken upon setting the property given property setter value, in the form of a /// function delegate, returning true if the action was successful.</param> /// <param name="overrideNotification">Detemines when listeners are notifies. /// By default, listeners are only notified when the property action is successful.</param> /// <param name="propertyName">Name of the property, as expected by listeners. This /// value is automatically provided by <see cref="CallerMemberNameAttribute"/>, if not explicity provided.</param> /// <returns>True if the property action was successful, otherwise false.</returns> protected bool SetProperty <T>(Func <bool> setPropertyAction, NotifyListeners overrideNotification = NotifyListeners.Default, [CallerMemberName] string propertyName = null, IEnumerable <string> additionalNames = null) { bool actionResult; if (ShouldNotify(setPropertyAction, out actionResult, overrideNotification: overrideNotification)) { NotifyPropertyChanged(propertyName, additionalNames); } return(actionResult); }
private bool ShouldNotify(Func <bool> evaluationFunction, out bool functionResult, NotifyListeners overrideNotification = NotifyListeners.Default) { functionResult = evaluationFunction(); if (functionResult) { if (overrideNotification != NotifyListeners.Never) { return(true); } return(false); } if (overrideNotification == NotifyListeners.Always) { return(true); } return(false); }
/// <summary> /// Notifies listerners the calling property is being changed and modifies and sets backing field. /// /// <para>Should be called in all bindable properties. Only notifies listeners when the property did not already equal the new value, /// unless otherwise stated using the <paramref name="overrideNotification"/> parameter.</para> /// </summary> /// <typeparam name="T">The property type.</typeparam> /// <param name="propertyRef">Reference to a property backing field.</param> /// <param name="value">New value for the property.</param> /// <param name="overrideNotification">Detemines when listeners are notifies. /// By default, listeners are only notified when the property value is indeed changed.</param> /// <param name="propertyName">Name of the property, as expected by listeners. This /// value is automatically provided by <see cref="CallerMemberNameAttribute"/>, if not explicity provided.</param> /// <param name="additionalNames">Names of addtional properties that will also notify listeners of change.</param> /// <returns>False if the property need not be changed, otherwise true.</returns> protected bool SetProperty <T>(ref T propertyRef, T value, NotifyListeners overrideNotification = NotifyListeners.Default, [CallerMemberName] string propertyName = null, IEnumerable <string> additionalNames = null) { var tempField = propertyRef; Func <bool> CheckNotEqual = () => !(object.Equals(tempField, value)); bool areNotEqual; bool notify = ShouldNotify(CheckNotEqual, out areNotEqual, overrideNotification: overrideNotification); if (areNotEqual) { propertyRef = value; //TEST valuechangedonevent: Check ref value mutation before notify } if (notify) { NotifyPropertyChanged(propertyName, additionalNames); } return(areNotEqual); }
public void SetProperty_ActionOverrideNotify_ListenersNotified(bool actionSuccess, NotifyListeners overrideNotification) { bool isNotifiedExpected; switch (overrideNotification) { case NotifyListeners.Always: isNotifiedExpected = true; break; case NotifyListeners.Never: isNotifiedExpected = false; break; default: Assert.Fail("No override given."); return; } var isNotifiedActual = false; Func <bool> testAction = () => actionSuccess; void ListenerFunction(object sender, PropertyChangedEventArgs e) { isNotifiedActual = true; } PropertyChanged += ListenerFunction; SetProperty <bool>(testAction, overrideNotification: overrideNotification); Assert.AreEqual(isNotifiedExpected, isNotifiedActual); PropertyChanged -= ListenerFunction; }
public void SetProperty_FieldValueOverrideNotify_ListenersNotified(bool startValue, bool setValue, NotifyListeners overrideNotification) { bool isNotifiedExpected; switch (overrideNotification) { case NotifyListeners.Always: isNotifiedExpected = true; break; case NotifyListeners.Never: isNotifiedExpected = false; break; default: Assert.Fail("No override given."); return; } var isNotifiedActual = false; var testField = startValue; void ListenerFunction(object sender, PropertyChangedEventArgs e) { isNotifiedActual = true; } PropertyChanged += ListenerFunction; SetProperty <bool>(ref testField, setValue, overrideNotification: overrideNotification); Assert.AreEqual(isNotifiedExpected, isNotifiedActual); PropertyChanged -= ListenerFunction; }