| MAUI-CommunityToolkit-10 :👈 | 👉:MAUI-Headless-Pattern |
Hour 11 β MAUI 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:
MAUI supports builtβin animations for transforming UI elements with smooth visual effects.
await myLabel.FadeTo(0, 500); await myLabel.FadeTo(1, 500);
β Creates a fade-out and fade-in effect.
await myButton.ScaleTo(1.2, 300); await myButton.ScaleTo(1.0, 300);
β Useful for "press" or "pop" effects.
await myImage.TranslateTo(50, 0, 400);
β Moves element smoothly across the screen.
await myIcon.RotateTo(360, 700);
β Full circle rotation.
Triggers change UI properties automatically when a condition is met.
<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.
<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.
Behaviors let you attach reusable logic to any control without subclassing.
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;
}
}
<Entry Placeholder="Enter number only">
<Entry.Behaviors>
<local:NumericEntryBehavior />
</Entry.Behaviors>
</Entry>
β Prevents non-numeric input globally.
Effects add platformβspecific enhancements without writing a full handler or renderer.
public class FocusEffect : RoutingEffect
{
public FocusEffect() : base("MyApp.FocusEffect")
{
}
}
// 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);
}
}
<Entry Text="Focus me">
<Entry.Effects>
<local:FocusEffect />
</Entry.Effects>
</Entry>
β Entry background changes only on Android (platform-specific).
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);
};
}
}
<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
| MAUI-CommunityToolkit-10 :👈 | 👉:MAUI-Headless-Pattern |