| 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
| MAUI-CommunityToolkit-10 :👈 | 👉:MAUI-Headless-Pattern |