using System;
using Avalonia.Animation;
using Avalonia.Styling;
using System.Reactive.Disposables;
namespace Avalonia.Xaml.Interactions.Custom;
///
///
///
public class FadeInBehavior : AttachedToVisualTreeBehavior
{
///
///
///
public static readonly StyledProperty InitialDelayProperty =
AvaloniaProperty.Register(nameof(InitialDelay), TimeSpan.FromMilliseconds(500));
///
///
///
public static readonly StyledProperty DurationProperty =
AvaloniaProperty.Register(nameof(Duration), TimeSpan.FromMilliseconds(250));
///
///
///
public TimeSpan InitialDelay
{
get => GetValue(InitialDelayProperty);
set => SetValue(InitialDelayProperty, value);
}
///
///
///
public TimeSpan Duration
{
get => GetValue(DurationProperty);
set => SetValue(DurationProperty, value);
}
///
///
///
///
protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
{
if (AssociatedObject is null)
{
return;
}
var totalDuration = InitialDelay + Duration;
var animation = new Animation.Animation
{
Duration = totalDuration,
Children =
{
new KeyFrame {KeyTime = TimeSpan.Zero, Setters = {new Setter(Visual.OpacityProperty, 0d),}},
new KeyFrame {KeyTime = InitialDelay, Setters = {new Setter(Visual.OpacityProperty, 0d),}},
new KeyFrame {KeyTime = Duration, Setters = {new Setter(Visual.OpacityProperty, 1d),}}
}
};
animation.RunAsync(AssociatedObject);
}
}