Browse Source

Fix KeyNotFound exception in Animatable.

When transitions are replaced, add the new transitions before removing the old transitions, so that when the old transition being disposed causes the value to change, there is a corresponding entry in `_transitionStates`.

Also search the `Transitions` collection from last to first to find a matching transition, so that later added transitions have priority.

Fixes #4059
Steven Kirk 5 years ago
parent
commit
0694b22c51
1 changed files with 9 additions and 7 deletions
  1. 9 7
      src/Avalonia.Animation/Animatable.cs

+ 9 - 7
src/Avalonia.Animation/Animatable.cs

@@ -93,17 +93,17 @@ namespace Avalonia.Animation
                 var oldTransitions = change.OldValue.GetValueOrDefault<Transitions>();
                 var newTransitions = change.NewValue.GetValueOrDefault<Transitions>();
 
-                if (oldTransitions is object)
-                {
-                    oldTransitions.CollectionChanged -= TransitionsCollectionChanged;
-                    RemoveTransitions(oldTransitions);
-                }
-
                 if (newTransitions is object)
                 {
                     newTransitions.CollectionChanged += TransitionsCollectionChanged;
                     AddTransitions(newTransitions);
                 }
+
+                if (oldTransitions is object)
+                {
+                    oldTransitions.CollectionChanged -= TransitionsCollectionChanged;
+                    RemoveTransitions(oldTransitions);
+                }
             }
             else if (_transitionsEnabled &&
                      Transitions is object &&
@@ -111,8 +111,10 @@ namespace Avalonia.Animation
                      !change.Property.IsDirect &&
                      change.Priority > BindingPriority.Animation)
             {
-                foreach (var transition in Transitions)
+                for (var i = Transitions.Count -1; i >= 0; --i)
                 {
+                    var transition = Transitions[i];
+
                     if (transition.Property == change.Property)
                     {
                         var state = _transitionState[transition];