diff --git a/source/SkiaSharp.Extended.UI/Controls/Confetti/SKConfettiView.shared.cs b/source/SkiaSharp.Extended.UI/Controls/Confetti/SKConfettiView.shared.cs index f48f1704..5aeb1575 100644 --- a/source/SkiaSharp.Extended.UI/Controls/Confetti/SKConfettiView.shared.cs +++ b/source/SkiaSharp.Extended.UI/Controls/Confetti/SKConfettiView.shared.cs @@ -23,7 +23,7 @@ public class SKConfettiView : SKAnimatedSurfaceView public SKConfettiView() { - Themes.SKConfettiViewResources.EnsureRegistered(); + ResourceLoader.EnsureRegistered(this); SizeChanged += OnSizeChanged; PropertyChanged += (_, e) => diff --git a/source/SkiaSharp.Extended.UI/Controls/Confetti/SKConfettiViewResources.shared.xaml.cs b/source/SkiaSharp.Extended.UI/Controls/Confetti/SKConfettiViewResources.shared.xaml.cs index 7b447fc8..e10040fc 100644 --- a/source/SkiaSharp.Extended.UI/Controls/Confetti/SKConfettiViewResources.shared.xaml.cs +++ b/source/SkiaSharp.Extended.UI/Controls/Confetti/SKConfettiViewResources.shared.xaml.cs @@ -2,13 +2,8 @@ public partial class SKConfettiViewResources : ResourceDictionary { - private static bool registered; - public SKConfettiViewResources() { InitializeComponent(); } - - internal static void EnsureRegistered() => - ResourceLoader.EnsureRegistered(ref registered); } diff --git a/source/SkiaSharp.Extended.UI/Controls/Lottie/SKLottieView.shared.cs b/source/SkiaSharp.Extended.UI/Controls/Lottie/SKLottieView.shared.cs index 60869f7c..196bf0c7 100644 --- a/source/SkiaSharp.Extended.UI/Controls/Lottie/SKLottieView.shared.cs +++ b/source/SkiaSharp.Extended.UI/Controls/Lottie/SKLottieView.shared.cs @@ -54,7 +54,7 @@ public class SKLottieView : SKAnimatedSurfaceView public SKLottieView() { - Themes.SKLottieViewResources.EnsureRegistered(); + ResourceLoader.EnsureRegistered(this); IsAnimationEnabled = true; } diff --git a/source/SkiaSharp.Extended.UI/Controls/Lottie/SKLottieViewResources.shared.xaml.cs b/source/SkiaSharp.Extended.UI/Controls/Lottie/SKLottieViewResources.shared.xaml.cs index 4e436be4..eb462c7e 100644 --- a/source/SkiaSharp.Extended.UI/Controls/Lottie/SKLottieViewResources.shared.xaml.cs +++ b/source/SkiaSharp.Extended.UI/Controls/Lottie/SKLottieViewResources.shared.xaml.cs @@ -2,13 +2,8 @@ public partial class SKLottieViewResources : ResourceDictionary { - private static bool registered; - public SKLottieViewResources() { InitializeComponent(); } - - internal static void EnsureRegistered() => - ResourceLoader.EnsureRegistered(ref registered); } diff --git a/source/SkiaSharp.Extended.UI/Utils/ResourceLoader.shared.cs b/source/SkiaSharp.Extended.UI/Utils/ResourceLoader.shared.cs index 10cbf772..b2a2814f 100644 --- a/source/SkiaSharp.Extended.UI/Utils/ResourceLoader.shared.cs +++ b/source/SkiaSharp.Extended.UI/Utils/ResourceLoader.shared.cs @@ -1,13 +1,16 @@ namespace SkiaSharp.Extended.UI; -internal static class ResourceLoader +internal static class ResourceLoader + where T : ResourceDictionary, new() { - internal static void EnsureRegistered(ref bool registered) - where T : ResourceDictionary, new() + private static bool registered; + + internal static void EnsureRegistered(VisualElement? element = null) { if (registered) return; + // try register with the current app var merged = Application.Current?.Resources?.MergedDictionaries; if (merged != null) { @@ -26,5 +29,26 @@ internal static void EnsureRegistered(ref bool registered) registered = true; } } + +#if !XAMARIN_FORMS + // the app may not be ready yet - if this page is part of the app's constructor + // so we will wait until this view gets a window, then we will try again + if (!registered && element != null) + { + element.PropertyChanged += OnPropertyChanged; + + static void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) + { + if (e.PropertyName != VisualElement.WindowProperty.PropertyName) + return; + + // the window changed, so try one more time + EnsureRegistered(); + + if (sender is VisualElement ve) + ve.PropertyChanged -= OnPropertyChanged; + } + } +#endif } }