=============================== WinUI 3 Application Integration =============================== WinUI 3 applications can leverage IoC.Configuration to manage complex dependency graphs and plugin architectures. Unlike ASP.NET or MAUI, WinUI 3 integration typically uses the default host builder implementations provided in the core library. How it works ------------ By using the host integration features, WinUI 3 applications can resolve their main windows, services, and navigation components through the IoC.Configuration container. This is achieved by wrapping the standard ``Microsoft.Extensions.Hosting.IHostBuilder`` with the `IoC.Configuration.DiContainerBuilder.ApplicationHostBuilder `_. Key steps include: - Initializing the standard .NET ``HostBuilder``. - Wrapping it in an ``ApplicationHostBuilder``. - Calling ``WithHostBuilder`` during the IoC.Configuration startup sequence to delegate service resolution. Example ------- For a complete implementation, refer to the example project ``WinUI3Demo.csproj`` and related projects in `ApplicationIntegrationDemos/WinUI3 `_: The code snippet below shows how the methods ``WithHostBuilder`` and ``RegisterServiceProviderAndBuildApp`` are used in `WinUI3Demo.App `_ to register the DI container configured in the IoC.Configuration file `IoCConfiguration.xml `_ with the WinUI 3 application host. .. note:: This setup allows WinUI 3 components like the ``MainWindow`` to have their dependencies automatically injected by the IoC.Configuration container. **App.xaml.cs**: .. sourcecode:: csharp //using Microsoft.Extensions.DependencyInjection; using IoC.Configuration; using IoC.Configuration.DiContainerBuilder; using IoC.Configuration.DiContainerBuilder.FileBased; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.UI.Xaml; using OROptimizer; using System; using OROptimizer.Diagnostics.Log; // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. namespace WinUI3Demo; /// /// Provides application-specific behavior to supplement the default Application class. /// public partial class App : Application { public static IHost? AppHost { get; private set; } /// /// Initializes the singleton application object. /// public App() { //Services = ConfigureServices(); this.InitializeComponent(); LogHelper.RegisterContext(new LogHelperContextLogToConsole()); ConfigureDi(); } private static void ConfigureDi() { var hostBuilder = Host.CreateDefaultBuilder(); var diContainerBuilder = new IoC.Configuration.DiContainerBuilder.DiContainerBuilder(); var fileBasedConfigurationParameters = new FileBasedConfigurationParameters( new FileBasedConfigurationFileContentsProvider(WinUI3Demo.Properties.Settings.Default.IoCConfigurationFilePath), AppContext.BaseDirectory, new AllLoadedAssemblies()) { AttributeValueTransformers = [new FileFolderPathAttributeValueTransformer()] }; var hostIntegratedContainerInfo = diContainerBuilder.StartFileBasedDi(fileBasedConfigurationParameters) .WithoutPresetDiContainer() // Add additional modules using AddAdditionalDiModules() one or multiple times as necessary // to register modules in addition to DI specified in "IoCConfiguration.xml" // If the method is not called, only the //.AddAdditionalDiModules(new MyModule()) // Use WithHostBuilder(hostBuilder) to make sure IoC.Configuration will register DI with the host builder // Do not call hostBuilder.Build() since this will be done by IoC.Configuration. .WithHostBuilder(new ApplicationHostBuilder(hostBuilder)) .RegisterServiceProviderAndBuildApp(); AppHost = hostIntegratedContainerInfo.Host; var diContainer = hostIntegratedContainerInfo.ContainerInfo.DiContainer; // From this point on either AppHost.Services or hostIntegratedContainerInfo.ContainerInfo.DiContainer // can be used to resolve services. Both will use the same DI container. } /// /// Invoked when the application is launched. /// protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) { // Resolve MainWindow from the DI container await (AppHost ?? throw new InvalidOperationException("Hos not set")).StartAsync(); var window = AppHost.Services.GetRequiredService(); window.Activate(); } } **File ``IoCConfiguration.xml``** located in the same directory as ``WinUI3Demo.csproj``. .. code-block:: xml