diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index 0e1882ce758..cf796401500 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -37,6 +37,7 @@ internal class AnimationInstance : SingleSubscriberObservableBase private IDisposable _timerSub; private readonly IClock _baseClock; private IClock _clock; + private EventHandler _propertyChangedDelegate; public AnimationInstance(Animation animation, Animatable control, Animator animator, IClock baseClock, Action OnComplete, Func Interpolator) { @@ -46,9 +47,6 @@ public AnimationInstance(Animation animation, Animatable control, Animator an _onCompleteAction = OnComplete; _interpolator = Interpolator; _baseClock = baseClock; - control.PropertyChanged += ControlPropertyChanged; - - UpdateNeutralValue(); FetchProperties(); } @@ -82,6 +80,7 @@ protected override void Unsubscribed() // Animation may have been stopped before it has finished. ApplyFinalFill(); + _targetControl.PropertyChanged -= _propertyChangedDelegate; _timerSub?.Dispose(); _clock.PlayState = PlayState.Stop; } @@ -90,6 +89,9 @@ protected override void Subscribed() { _clock = new Clock(_baseClock); _timerSub = _clock.Subscribe(Step); + _propertyChangedDelegate ??= ControlPropertyChanged; + _targetControl.PropertyChanged += _propertyChangedDelegate; + UpdateNeutralValue(); } public void Step(TimeSpan frameTick)