MAUI-CommunityToolkit-10 :👈 👉:MAUI-Headless-Pattern

Hour 11 – MAUI Animations, Triggers, Behaviors & Effects

πŸ“˜ Hour 11 – Animations, Triggers, Behaviors & Effects

Welcome to Hour 11 of your MAUI MVVM learning series. Today we explore four powerful UI enhancement tools in .NET MAUI:

  • ➑ Animations
  • ➑ Triggers
  • ➑ Behaviors
  • ➑ Effects

✨ 1. MAUI Animations

MAUI supports built‑in animations for transforming UI elements with smooth visual effects.

πŸ”„ 1.1 Fade Animation

await myLabel.FadeTo(0, 500);
await myLabel.FadeTo(1, 500);

βœ” Creates a fade-out and fade-in effect.

πŸ” 1.2 Scale Animation

await myButton.ScaleTo(1.2, 300);
await myButton.ScaleTo(1.0, 300);

βœ” Useful for "press" or "pop" effects.

πŸ” 1.3 Translate Animation

await myImage.TranslateTo(50, 0, 400);

βœ” Moves element smoothly across the screen.

πŸŒ€ 1.4 Rotation

await myIcon.RotateTo(360, 700);

βœ” Full circle rotation.


✨ 2. Triggers in MAUI

Triggers change UI properties automatically when a condition is met.

πŸŽ› 2.1 Property Trigger

<Entry>
    <Entry.Triggers>
        <Trigger TargetType="Entry" Property="IsFocused" Value="True">
            <Setter Property="BackgroundColor" Value="LightYellow" />
        </Trigger>
    </Entry.Triggers>
</Entry>

βœ” Changes background when entry is focused.

πŸ“˜ 2.2 Data Trigger

<Label Text="{Binding Status}">
    <Label.Triggers>
        <DataTrigger TargetType="Label"
                     Binding="{Binding IsError}"
                     Value="True">
            <Setter Property="TextColor" Value="Red" />
        </DataTrigger>
    </Label.Triggers>
</Label>

βœ” Data-binding based UI reaction.


✨ 3. Behaviors in MAUI

Behaviors let you attach reusable logic to any control without subclassing.

🧩 3.1 Example: Text Only Numeric Behavior

public class NumericEntryBehavior : Behavior<Entry>
{
    protected override void OnAttachedTo(Entry entry)
    {
        entry.TextChanged += Entry_TextChanged;
        base.OnAttachedTo(entry);
    }

    private void Entry_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (!int.TryParse(e.NewTextValue, out _))
            ((Entry)sender).Text = e.OldTextValue;
    }
}

πŸ§ͺ 3.2 Attach Behavior in XAML

<Entry Placeholder="Enter number only">
    <Entry.Behaviors>
        <local:NumericEntryBehavior />
    </Entry.Behaviors>
</Entry>

βœ” Prevents non-numeric input globally.


✨ 4. Effects in MAUI

Effects add platform‑specific enhancements without writing a full handler or renderer.

πŸ’‘ 4.1 Create Routing Effect

public class FocusEffect : RoutingEffect
{
    public FocusEffect() : base("MyApp.FocusEffect")
    {
    }
}

⬇ 4.2 Platform Implementation (Android)

// Platforms/Android/Effects/FocusEffect.cs
[assembly: ResolutionGroupName("MyApp")]
[assembly: ExportEffect(typeof(FocusEffectDroid), "FocusEffect")]

public class FocusEffectDroid : PlatformEffect
{
    protected override void OnAttached()
    {
        Control.SetBackgroundColor(Android.Graphics.Color.LightGreen);
    }

    protected override void OnDetached()
    {
        Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
    }
}

βš™ 4.3 Use Effect in XAML

<Entry Text="Focus me">
    <Entry.Effects>
        <local:FocusEffect />
    </Entry.Effects>
</Entry>

βœ” Entry background changes only on Android (platform-specific).


✨ 5. Combined Example – Animated Button With Behavior and Trigger

πŸ“„ 5.1 Behavior: Grow Button When Pressed

public class GrowOnPressBehavior : Behavior<Button>
{
    protected override void OnAttachedTo(Button button)
    {
        button.Pressed += async (s, e) =>
        {
            await button.ScaleTo(1.2, 150);
        };

        button.Released += async (s, e) =>
        {
            await button.ScaleTo(1.0, 150);
        };
    }
}

πŸ“„ 5.2 XAML Usage

<Button Text="Tap Me">
    <Button.Behaviors>
        <local:GrowOnPressBehavior />
    </Button.Behaviors>

    <Button.Triggers>
        <Trigger TargetType="Button" Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.5" />
        </Trigger>
    </Button.Triggers>
</Button>

βœ” Smooth animations βœ” Visual feedback βœ” Declarative UI behavior βœ” Highly reusable


πŸ“˜ Hour 11 Summary

  • βœ” Animations enhance user experience with movement & transitions
  • βœ” Triggers react to property or data changes automatically
  • βœ” Behaviors add reusable logic to any control
  • βœ” Effects provide light platform-specific enhancements
  • βœ” Together, they create rich, polished MAUI UI
Back to Index
MAUI-CommunityToolkit-10 :👈 👉:MAUI-Headless-Pattern
*