In .NET MAUI I am creating an animation to change the size of an element inside an AbsoluteLayout
, I create the animation like so :
_animationProgress = new Animation(v => AbsoluteContainer.SetLayoutBounds(RectangleProgress, new Rect() { Height = 1, Width = v}), 0, 1);
_animationProgress.Commit(
this,
"TrackListSideBarAnimation",
16,
DurationMs,
Easing.Linear,
OnProgressComplete);
The user can stop the animation at any time and I don’t want the OnProgressComplete
to fire then. I tried to clean the animation in different way but the OnProgressComplete
keep firing, what I tried :
public void Stop()
{
if (_animationProgression != null)
{
_animationProgress.Finished = null;
_animationProgress.Pause();
_animationProgress.RemoveFromParent();
_animationProgress.Dispose();
_animationProgress = null;
}
}
I can still ignore the call inside OnProgressComplete
if _animationProgress
is null but the fact that the event is still up even after Dispose
seems pretty strange. Is it supposed to be like that ? Is there a way to clean entirely an animation after a commit ?
2
You can call view.AbortAnimation(animationName)
to cancel an animation, it will still fire the finished event. So I made the following changes :
_animationProgress.Commit(
RectangleProgress, // Pass the element I am animating on
"TrackListSideBarAnimation",
16,
DureeMillisecondes,
Easing.Linear,
OnProgressComplete);
Then to stop the animation :
if (_animationProgress != null)
{
_animationProgress.Dispose();
_animationProgress = null;
RectangleProgress.AbortAnimation("TrackListSideBarAnimation");
}
Which will call OnProgressComplete
where I am handling things like that :
private void OnProgressComplete(double v, bool c)
{
if (_animationProgress != null)
{
ProgressionFinished?.Invoke();
_animationProgress = null;
}
}
Pretty strange that you can’t do that from the animation though.