From fd9eff6c439073d7da717d8aec75c1cb372e6dda Mon Sep 17 00:00:00 2001 From: jsy Date: Tue, 21 Nov 2023 17:33:48 +0900 Subject: [PATCH] add dp Sourcestream --- .../LottieSharp.WPF/LottieAnimationView.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/LottieSharp/LottieSharp.WPF/LottieAnimationView.cs b/src/LottieSharp/LottieSharp.WPF/LottieAnimationView.cs index 0c21f9b..8132298 100644 --- a/src/LottieSharp/LottieSharp.WPF/LottieAnimationView.cs +++ b/src/LottieSharp/LottieSharp.WPF/LottieAnimationView.cs @@ -27,6 +27,12 @@ public AnimationInfo Info public event EventHandler OnStop; + public Stream SourceStream + { + get => (Stream)GetValue(SourceStreamProperty); + set => SetValue(SourceStreamProperty, value); + } + public string FileName { get => (string)GetValue(FileNameProperty); @@ -119,12 +125,23 @@ public RepeatMode Repeat public static readonly DependencyProperty RepeatProperty = DependencyProperty.Register("Repeat", typeof(RepeatMode), typeof(LottieAnimationView), new PropertyMetadata(RepeatMode.Restart)); + public static readonly DependencyProperty SourceStreamProperty = + DependencyProperty.Register("SourceStream", typeof(Stream), typeof(LottieAnimationView), new PropertyMetadata(null, SourceStreamPropertyChangedCallback)); + public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(LottieAnimationView), new PropertyMetadata(null, FileNamePropertyChangedCallback)); public static readonly DependencyProperty ResourcePathProperty = DependencyProperty.Register("ResourcePath", typeof(string), typeof(LottieAnimationView), new PropertyMetadata(null, ResourcePathPropertyChangedCallback)); + private static void SourceStreamPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) + { + if (dependencyObject is LottieAnimationView lottieAnimationView && e.NewValue is Stream stream) + { + lottieAnimationView.SetAnimationFromStream(stream); + } + } + private static void FileNamePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { if (dependencyObject is LottieAnimationView lottieAnimationView && e.NewValue is string assetName) @@ -141,6 +158,19 @@ private static void ResourcePathPropertyChangedCallback(DependencyObject depende } } + private void SetAnimationFromStream(Stream stream) + { + try + { + SetAnimation(stream); + } + catch (Exception) + { + Debug.WriteLine($"Unexpected error when loading {stream}"); + throw; + } + } + private void SetAnimationFromFile(string assetName) { try