Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion docs/user-interface/animation/basic.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Basic animation"
description: "The .NET MAUI ViewExtensions class, in the Microsoft.Maui.Controls namespace, provides extension methods that can be used to create and cancel basic animations."
ms.date: 04/03/2025
ms.date: 06/05/2026
---

# Basic animation
Expand All @@ -27,3 +27,40 @@ image.CancelAnimations();
```

In this example, all animations that are running on the <xref:Microsoft.Maui.Controls.Image> instance are immediately canceled.

:::moniker range=">=net-maui-11.0"

Comment thread
jfversluis marked this conversation as resolved.
Resolved
### Cancel an animation with a CancellationToken

Starting in .NET MAUI 11, the `ViewExtensions` animation methods (`FadeToAsync`, `RotateToAsync`, `ScaleToAsync`, `TranslateToAsync`, and the relative variants) accept an optional <xref:System.Threading.CancellationToken>. Passing a token lets you cancel a specific awaited animation without having to call `CancelAnimations`, which cancels every animation on the element. This is useful when an element is running more than one animation, or when an animation should stop in response to a separate user action.

The cancellation-aware overloads return `Task<bool>`. The result is `true` when the animation was canceled and `false` when it ran to completion. The animation methods don't throw on cancellation, so there's no exception to catch.

The following example fades a label out, and cancels the fade if the user presses a button before it completes:

```csharp
CancellationTokenSource cts = new();

async void OnFadeClicked(object sender, EventArgs e)
{
bool canceled = await label.FadeToAsync(0, 2000, Easing.SinIn, cts.Token);

if (canceled)
{
// The fade was canceled before it completed; the label keeps
// whatever opacity it had reached.
}
}

void OnCancelClicked(object sender, EventArgs e)
{
cts.Cancel();
}
Comment thread
jfversluis marked this conversation as resolved.
Resolved
```

When the token is canceled, the underlying animation is aborted and the element keeps whatever intermediate value it had reached. A typical pattern is to read the `bool` return value and either reset the element to a known state or animate it from where it stopped.

> [!NOTE]
> The non-`Async` versions of these methods (`FadeTo`, `RotateTo`, and so on) are now marked `[Obsolete]` in favor of the `Async`-suffixed equivalents. Migrate to the new methods to take advantage of `CancellationToken` support.

:::moniker-end
Comment thread
jfversluis marked this conversation as resolved.
Resolved
Loading