Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 42 ยท Styling and Resources

Animations and Custom Handlers

Animate .NET MAUI controls with ViewExtensions, Animation objects and easing, then customize native controls with handler mappings; includes runnable easing and animation timeline stand-ins.

Small, purposeful motion tells users what happened: a card slides in, a heart pulses after a tap, an error field shakes. MAUI includes a simple animation API for view properties. When a control is not enough and you need native behavior, handlers let you reach the platform control and change it. This lesson covers both.

MAUI runs only on devices, so the framework calls are read-only. The stand-ins implement easing functions and an animation timeline in plain C#, which shows what the framework computes each frame.

Simple animations

VisualElement has extension methods that animate one property and return a Task<bool> you can await:

C#
await image.FadeTo(1, 400);              // opacity to 1 over 400 ms
await image.TranslateTo(0, -40, 300);    // move up 40 units
await image.ScaleTo(1.2, 150);           // grow to 120%
await image.RotateTo(360, 500);          // turn once
await image.ScaleTo(1.0, 150);

Each call finishes before the next starts, so awaiting them in order builds a sequence. To run several at once, use Task.WhenAll:

C#
await Task.WhenAll(
    card.FadeTo(1, 300),
    card.TranslateTo(0, 0, 300, Easing.CubicOut));

Other helpers: RelRotateTo, RotateXTo, RotateYTo, LayoutTo, ScaleXTo, ScaleYTo and CancelAnimations(). The final bool is true when the animation was cancelled.

Starting values

Animate from a known start. Set the start in code before the page appears:

C#
protected override async void OnAppearing()
{
    base.OnAppearing();
    card.Opacity = 0;
    card.TranslationY = 30;
    await Task.WhenAll(card.FadeTo(1, 350), card.TranslateTo(0, 0, 350, Easing.CubicOut));
}

Easing

Without easing, motion is linear and looks mechanical. An easing function maps linear time t (0 to 1) to progress. Built-ins include Linear, SinIn, SinOut, SinInOut, CubicIn, CubicOut, CubicInOut, BounceIn, BounceOut and SpringIn, SpringOut. A custom one is new Easing(t => t * t).

C# Stand-in: easing functions as a table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

CubicOut rushes ahead early and settles gently, which feels natural for things entering the screen. CubicIn is the opposite and suits things leaving. Use around 200 to 400 ms for most UI motion.

The Animation class

For complex timing, build Animation objects. Each has a callback that receives a value from 0 to 1 (or between your start and end), and children can occupy part of the total time:

C#
var animation = new Animation();
animation.Add(0.0, 0.5, new Animation(v => box.Scale = v, 1, 1.5, Easing.CubicOut));
animation.Add(0.5, 1.0, new Animation(v => box.Scale = v, 1.5, 1, Easing.CubicIn));
animation.Add(0.0, 1.0, new Animation(v => box.Rotation = v, 0, 360));

animation.Commit(box, "Pulse", length: 800, repeat: () => true);   // loops until cancelled
// box.AbortAnimation("Pulse");

Add(begin, end, child) places the child inside the parent's time span: here the scale grows in the first half and shrinks in the second while the rotation lasts the whole time. The next stand-in simulates exactly that timeline at fixed frames.

C# Stand-in: an animation timeline sampled per frame

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The framework does the same thing 60 times a second, calling your lambdas with the interpolated value.

Animation tips

  • Animate transforms and opacity (Translation, Scale, Rotation, Opacity). They do not trigger a layout. Animating WidthRequest or Margin forces the whole layout to recompute each frame.
  • Always cancel when leaving a page: this.AbortAnimation("name") or view.CancelAnimations().
  • Respect users who turn animations off in the system's accessibility settings; keep animations short and optional.
  • Do not animate everything; one meaningful motion per action is enough.

Handlers

Every MAUI control (Entry, Button, Label) is a cross-platform virtual view. At run time a handler creates the native control (EditText on Android, UITextField on iOS, TextBox on Windows) and connects its properties. Because the handler is exposed, you can change the native control:

C#
// MauiProgram.cs
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoUnderline", (handler, view) =>
{
#if ANDROID
    handler.PlatformView.BackgroundTintList =
        Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Transparent);
#elif IOS || MACCATALYST
    handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
#elif WINDOWS
    handler.PlatformView.BorderThickness = new Microsoft.UI.Xaml.Thickness(0);
#endif
});

The mapper is a dictionary from property names (Text, TextColor, Background) to actions. AppendToMapping adds your code after the default, PrependToMapping before it, and ModifyMapping replaces the default action. Your change applies to every Entry in the app. To target only some, check the view type:

C#
public class BorderlessEntry : Entry { }

EntryHandler.Mapper.AppendToMapping("Borderless", (handler, view) =>
{
    if (view is BorderlessEntry) { /* change native control */ }
});

The #if ANDROID blocks are the compile-time tool from Platform-Specific Code, and PlatformView is the native object.

Mapper in plain C#

A mapper is a simple idea, and you can build one to understand it:

C# Stand-in: a tiny handler mapper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

When to use what

  • Style, trigger, state: look changes using existing properties. Try these first.
  • Behavior (Behavior<T>): attach reusable logic, such as numeric-only entries.
  • Handler mapping: the native control needs a change MAUI has no property for.
  • Custom handler (a new ViewHandler<TVirtual, TNative>): you want an entirely new control backed by native views. Rare, and a lot of work.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which properties are cheapest to animate?

    Transform and opacity changes do not force a layout pass. Changing size or margins recalculates layout every frame.
  2. 2. What does an easing function do?

    An easing takes t from 0 to 1 and returns the eased progress, so motion can start slow, finish slow, bounce and so on.
  3. 3. What does Handler.Mapper.AppendToMapping do?

    AppendToMapping adds an action that runs after the built-in mapping; PrependToMapping runs before it and ModifyMapping replaces it.
  4. 4. What is PlatformView in a handler?

    The handler's PlatformView is the real native control, so platform code can set native-only properties.

Summary

  • FadeTo, TranslateTo, ScaleTo and RotateTo are awaitable; combine with Task.WhenAll.
  • Easing shapes the motion; Animation objects with children give precise timelines.
  • Animate transforms and opacity, cancel animations when leaving, keep motion short.
  • Handlers connect a virtual MAUI view to a native control; mappers let you customize the native control per property.

Next: Unit Testing View Models