using System; using System.Threading; using Avalonia.Animation; using Avalonia.Controls; using Avalonia.Styling; namespace Avalonia.ReactiveUI { /// /// A ContentControl that animates the transition when its content is changed. /// [Obsolete("Use TransitioningContentControl in Avalonia.Controls namespace")] public class TransitioningContentControl : ContentControl, IStyleable { /// /// for the property. /// public static readonly StyledProperty PageTransitionProperty = AvaloniaProperty.Register(nameof(PageTransition), new CrossFade(TimeSpan.FromSeconds(0.5))); /// /// for the property. /// public static readonly StyledProperty DefaultContentProperty = AvaloniaProperty.Register(nameof(DefaultContent)); private CancellationTokenSource? _lastTransitionCts; /// /// Gets or sets the animation played when content appears and disappears. /// public IPageTransition? PageTransition { get => GetValue(PageTransitionProperty); set => SetValue(PageTransitionProperty, value); } /// /// Gets or sets the content displayed whenever there is no page currently routed. /// public object? DefaultContent { get => GetValue(DefaultContentProperty); set => SetValue(DefaultContentProperty, value); } /// /// Gets or sets the content with animation. /// public new object? Content { get => base.Content; set => UpdateContentWithTransition(value); } /// /// TransitioningContentControl uses the default ContentControl /// template from Avalonia default theme. /// Type IStyleable.StyleKey => typeof(ContentControl); /// /// Updates the content with transitions. /// /// New content to set. private async void UpdateContentWithTransition(object? content) { _lastTransitionCts?.Cancel(); _lastTransitionCts = new CancellationTokenSource(); if (PageTransition != null) await PageTransition.Start(this, null, true, _lastTransitionCts.Token); base.Content = content; if (PageTransition != null) await PageTransition.Start(null, this, true, _lastTransitionCts.Token); } } }