Skip to content

Commit

Permalink
Add dependency injection methods for WPF controls
Browse files Browse the repository at this point in the history
Extended ServiceCollectionExtensions with methods for ContentControl, Control, UserControl, and FrameworkElement. These methods ensure proper dependency injection registration to enhance WPF application modularity and maintainability.
  • Loading branch information
frankhaugen committed Aug 5, 2024
1 parent 8404c59 commit 04d1b10
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Frank.Wpf.Hosting/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddPage<T>(this IServiceCollection services) where T : Page
{
services.AddTransient<T>();
services.AddTransient<Page, T>(provider => provider.GetRequiredService<T>());
return services;
}

Expand All @@ -22,9 +23,31 @@ public static IServiceCollection AddWindow<T>(this IServiceCollection services)
return services;
}

public static IServiceCollection AddContentControl<T>(this IServiceCollection services) where T : ContentControl
{
services.AddTransient<T>();
services.AddTransient<ContentControl, T>(provider => provider.GetRequiredService<T>());
return services;
}

public static IServiceCollection AddControl<T>(this IServiceCollection services) where T : Control
{
services.AddTransient<T>();
services.AddTransient<Control, T>(provider => provider.GetRequiredService<T>());
return services;
}

public static IServiceCollection AddUserControl<T>(this IServiceCollection services) where T : UserControl
{
services.AddTransient<T>();
services.AddTransient<UserControl, T>(provider => provider.GetRequiredService<T>());
return services;
}

public static IServiceCollection AddFrameworkElement<T>(this IServiceCollection services) where T : FrameworkElement
{
services.AddTransient<T>();
services.AddTransient<FrameworkElement, T>(provider => provider.GetRequiredService<T>());
return services;
}
}

0 comments on commit 04d1b10

Please sign in to comment.