Browse Source

Only call property notify on effective value change.

Steven Kirk 5 years ago
parent
commit
c872cc005d

+ 8 - 2
src/Avalonia.Base/AvaloniaObject.cs

@@ -716,7 +716,10 @@ namespace Avalonia
         {
             VerifyAccess();
 
-            change.Property.Notifying?.Invoke(this, true);
+            if (change.IsEffectiveValueChange)
+            {
+                change.Property.Notifying?.Invoke(this, true);
+            }
 
             try
             {
@@ -747,7 +750,10 @@ namespace Avalonia
             }
             finally
             {
-                change.Property.Notifying?.Invoke(this, false);
+                if (change.IsEffectiveValueChange)
+                {
+                    change.Property.Notifying?.Invoke(this, false);
+                }
             }
         }
 

+ 19 - 1
tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs

@@ -102,6 +102,17 @@ namespace Avalonia.Base.UnitTests
             Assert.Equal(new[] { "animated" }, result);
         }
 
+        [Fact]
+        public void Notify_Fired_Only_On_Effective_Value_Change()
+        {
+            var target = new Class1();
+
+            target.SetValue(Class1.FooProperty, "animated", BindingPriority.Animation);
+            target.SetValue(Class1.FooProperty, "local");
+
+            Assert.Equal(2, target.NotifyCount);
+        }
+
         [Fact]
         public void Property_Equals_Should_Handle_Null()
         {
@@ -180,7 +191,14 @@ namespace Avalonia.Base.UnitTests
         private class Class1 : AvaloniaObject
         {
             public static readonly StyledProperty<string> FooProperty =
-                AvaloniaProperty.Register<Class1, string>("Foo", "default");
+                AvaloniaProperty.Register<Class1, string>("Foo", "default", notifying: FooNotifying);
+
+            public int NotifyCount { get; private set; }
+
+            private static void FooNotifying(IAvaloniaObject o, bool n)
+            {
+                ++((Class1)o).NotifyCount;
+            }
         }
 
         private class Class2 : Class1