From 8bbfc415e6fb3c7714d1d4e56c87a77b75a9ab84 Mon Sep 17 00:00:00 2001 From: Dan Clark Date: Thu, 5 Dec 2024 16:11:48 +0000 Subject: [PATCH] Fixed InitializationScoped --- Toolkit.Foundation/IInitializationScoped.cs | 6 ++---- .../IServiceCollectionExtensions.cs | 20 ++++++++++++++++++- Toolkit.UI.WinUI/ContentTemplateBinding.cs | 14 +++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Toolkit.Foundation/IInitializationScoped.cs b/Toolkit.Foundation/IInitializationScoped.cs index 7b4d892..5c1675c 100644 --- a/Toolkit.Foundation/IInitializationScoped.cs +++ b/Toolkit.Foundation/IInitializationScoped.cs @@ -1,6 +1,4 @@ namespace Toolkit.Foundation; -public interface IInitializationScoped -{ - void Initialize(); -} \ No newline at end of file +public interface IInitializationScoped : + IInitialization; \ No newline at end of file diff --git a/Toolkit.Foundation/IServiceCollectionExtensions.cs b/Toolkit.Foundation/IServiceCollectionExtensions.cs index 4b747b3..eb878b0 100644 --- a/Toolkit.Foundation/IServiceCollectionExtensions.cs +++ b/Toolkit.Foundation/IServiceCollectionExtensions.cs @@ -62,12 +62,16 @@ public static class IServiceCollectionExtensions if (key is { Length: > 0 }) { services.Add(new ServiceDescriptor(typeof(IAsyncHandler), key, typeof(THandler), lifetime)); + services.AddInitializationScoped>>(key); + services.AddInitialization>>(key); } else { services.Add(new ServiceDescriptor(typeof(IAsyncHandler), typeof(THandler), lifetime)); + services.AddInitializationScoped>>(); + services.AddInitialization>>(); } return services; @@ -85,12 +89,16 @@ public static class IServiceCollectionExtensions if (key is { Length: > 0 }) { services.Add(new ServiceDescriptor(typeof(IAsyncHandler), key, typeof(THandler), lifetime)); + services.AddInitializationScoped>>(key); + services.AddInitialization>>(key); } else { services.Add(new ServiceDescriptor(typeof(IAsyncHandler), typeof(THandler), lifetime)); + services.AddInitializationScoped>>(); + services.AddInitialization>>(); } return services; @@ -220,12 +228,16 @@ public static class IServiceCollectionExtensions if (key is { Length: > 0 }) { services.Add(new ServiceDescriptor(typeof(IHandler), key, typeof(THandler), lifetime)); + services.AddInitializationScoped>>(key); + services.AddInitialization>>(key); } else { services.Add(new ServiceDescriptor(typeof(IHandler), typeof(THandler), lifetime)); + services.AddInitializationScoped>>(); + services.AddInitialization>>(); } return services; @@ -240,15 +252,21 @@ public static class IServiceCollectionExtensions string? key = null) where THandler : class, IHandler where TMessage : class { + services.AddHandler(lifetime, key); + if (key is { Length: > 0 }) { services.Add(new ServiceDescriptor(typeof(IHandler), key, typeof(THandler), lifetime)); + services.AddInitializationScoped>>(key); + services.AddInitialization>>(key); } else { services.Add(new ServiceDescriptor(typeof(IHandler), typeof(THandler), lifetime)); + services.AddInitializationScoped>>(); + services.AddInitialization>>(); } return services; @@ -294,7 +312,7 @@ public static class IServiceCollectionExtensions where TInitializationImplementation : class, IInitializationScoped { services.Add(new ServiceDescriptor(typeof(IInitializationScoped), typeof(TInitializationImplementation), lifetime)); - services.AddTransient(provider => provider.GetRequiredService()); + services.AddTransient(provider => provider.GetRequiredService()); return services; } diff --git a/Toolkit.UI.WinUI/ContentTemplateBinding.cs b/Toolkit.UI.WinUI/ContentTemplateBinding.cs index 20a0195..869e92f 100644 --- a/Toolkit.UI.WinUI/ContentTemplateBinding.cs +++ b/Toolkit.UI.WinUI/ContentTemplateBinding.cs @@ -50,6 +50,18 @@ public static class ContentTemplateBinding { cachedActivation = null; content.Unloaded -= HandleUnloaded; + content.DataContextChanged -= HandleDataContextChanged; + } + } + + void HandleDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args) + { + if (sender is FrameworkElement content) + { + if (content.DataContext is IActivation activation) + { + activation.IsActive = true; + } } } @@ -57,11 +69,13 @@ public static class ContentTemplateBinding { content.Loaded += HandleLoaded; content.Unloaded += HandleUnloaded; + content.DataContextChanged += HandleDataContextChanged; } else { content.Loaded -= HandleLoaded; content.Unloaded -= HandleUnloaded; + content.DataContextChanged -= HandleDataContextChanged; } } }