Primary APIs:
Animatable.TransitionsTransitionsITransition and built-in transition typesAnimation, KeyFrame, Cue, RunAsyncIPageTransition, CrossFade, PageSlideTopLevel.RequestAnimationFrame(Action<TimeSpan>)MediaContext.BeginInvokeOnRender(Action)Reference source files:
src/Avalonia.Base/Animation/Animatable.cssrc/Avalonia.Base/Animation/Animation.cssrc/Avalonia.Base/Animation/CrossFade.cssrc/Avalonia.Base/Animation/PageSlide.cssrc/Avalonia.Controls/TopLevel.cssrc/Avalonia.Base/Media/MediaContext.csUse transitions for implicit animation when a property value changes.
Common transition types:
DoubleTransitionColorTransitionThicknessTransitionTransformOperationsTransitionExample:
<Button>
<Button.Transitions>
<Transitions>
<TransformOperationsTransition Property="RenderTransform" Duration="0:0:0.1" />
</Transitions>
</Button.Transitions>
</Button>
Use Animation when you need explicit timeline control.
var animation = new Animation
{
Duration = TimeSpan.FromMilliseconds(220),
Easing = new Avalonia.Animation.Easings.CubicEaseOut(),
Children =
{
new KeyFrame
{
Cue = new Cue(0d),
Setters = { new Setter(Visual.OpacityProperty, 0d) }
},
new KeyFrame
{
Cue = new Cue(1d),
Setters = { new Setter(Visual.OpacityProperty, 1d) }
}
}
};
await animation.RunAsync(myControl);
Useful knobs:
DurationIterationCountPlaybackDirectionFillModeDelayDelayBetweenIterationsSpeedRatioUse IPageTransition for view switching.
Built-ins:
CrossFadePageSlideCompositePageTransitionPageSlide expects source and destination visuals to share parent (see implementation constraints).
For request-frame animation loops in app code:
TopLevel.RequestAnimationFrame to schedule per-frame callbacks.MediaContext.BeginInvokeOnRender for render-loop callback injection (infrastructure-level API).Pattern:
void Start(TopLevel top)
{
void Tick(TimeSpan now)
{
UpdateSimulation(now);
InvalidateVisual();
top.RequestAnimationFrame(Tick);
}
top.RequestAnimationFrame(Tick);
}
Keep loops stoppable (window detach, view disposed, cancellation token).
ElementComposition) for advanced/high-frequency effects.PageSlide, ensure both visuals share the same parent.Default mode:
XAML-first complete example:
<Button xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="PulseButton"
Content="Animate"
Opacity="0.7">
<Button.Transitions>
<Transitions>
<DoubleTransition Property="Opacity" Duration="0:0:0.2" />
</Transitions>
</Button.Transitions>
</Button>
Code-only alternative (on request):
using Avalonia;
using Avalonia.Animation;
using Avalonia.Animation.Easings;
using Avalonia.Styling;
var fade = new Animation
{
Duration = TimeSpan.FromMilliseconds(200),
Easing = new CubicEaseOut(),
Children =
{
new KeyFrame { Cue = new Cue(0d), Setters = { new Setter(Visual.OpacityProperty, 0.7d) } },
new KeyFrame { Cue = new Cue(1d), Setters = { new Setter(Visual.OpacityProperty, 1.0d) } }
}
};
await fade.RunAsync(pulseButton);