using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Toolkit.Foundation; using Toolkit.UI.Controls.Avalonia; namespace Toolkit.Avalonia; public static class IServiceCollectionExtensions { public static IServiceCollection AddConfigurationTemplate(this IServiceCollection services, params object[]? parameters) where TConfiguration : class where THeader : class where TDescription : class where TAction : class { Type viewModelType = typeof(ComponentConfigurationViewModel); Type viewType = typeof(Button); object key = viewModelType.Name.Replace("ViewModel", ""); services.AddTransient>(provider => provider.GetRequiredService() .Create>(parameters)!); services.TryAddTransient(viewType); services.AddKeyedTransient>(key, (provider, key) => provider.GetRequiredService() .Create>(parameters)!); services.TryAddKeyedTransient(viewType, key); services.AddTransient(provider => new ContentTemplateDescriptor(key, viewModelType, viewType, parameters)); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); return services; } public static IServiceCollection AddConfigurationTemplate(this IServiceCollection services, Func valueDelegate, object header, object description, params object[]? parameters) where TConfiguration : class where TAction : class { Type viewModelType = typeof(ComponentConfigurationViewModel); Type viewType = typeof(Button); object key = viewModelType.Name.Replace("ViewModel", ""); parameters = [valueDelegate, header, description, .. parameters ?? Enumerable.Empty()]; services.AddTransient>(provider => provider.GetRequiredService() .Create>(parameters)!); services.TryAddTransient(viewType); services.AddKeyedTransient>(key, (provider, key) => provider.GetRequiredService() .Create>(parameters)!); services.TryAddKeyedTransient(viewType, key); services.AddTransient(provider => new ContentTemplateDescriptor(key, viewModelType, viewType, parameters)); services.TryAddTransient(); return services; } public static IServiceCollection AddConfigurationTemplate(this IServiceCollection services, Func valueDelegate, object description, params object[]? parameters) where TConfiguration : class where TDescription : class where TAction : class { Type viewModelType = typeof(ComponentConfigurationViewModel); Type viewType = typeof(Button); object key = viewModelType.Name.Replace("ViewModel", ""); parameters = [valueDelegate, description, .. parameters ?? Enumerable.Empty()]; services.AddTransient>(provider => provider.GetRequiredService() .Create>(parameters)!); services.TryAddTransient(viewType); services.AddKeyedTransient>(key, (provider, key) => provider.GetRequiredService() .Create>(parameters)!); services.TryAddKeyedTransient(viewType, key); services.AddTransient(provider => new ContentTemplateDescriptor(key, viewModelType, viewType, parameters)); services.TryAddTransient(); services.TryAddTransient(); return services; } public static IServiceCollection AddAvalonia(this IServiceCollection services) { services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddHandler(); services.AddHandler(nameof(IClassicDesktopStyleApplicationLifetime)); services.AddHandler(nameof(ISingleViewApplicationLifetime)); services.AddHandler(nameof(ContentControl)); services.AddHandler(nameof(Frame)); services.AddHandler(nameof(ContentDialog)); services.AddScoped(provider => new NavigationRegionCollection { { typeof(IClassicDesktopStyleApplicationLifetime), typeof(IClassicDesktopStyleApplicationLifetime) }, { typeof(ISingleViewApplicationLifetime), typeof(ISingleViewApplicationLifetime) } }); services.AddTransient((Func>)(provider => new ProxyServiceCollection(services => { services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddSingleton(provider.GetRequiredService()); services.AddTransient(); services.AddTransient(); services.AddHandler(); services.AddHandler(nameof(ContentControl)); services.AddHandler(nameof(Frame)); services.AddHandler(nameof(ContentDialog)); }))); return services; } }