From 18470554cf2aa8046d0fff34a6dc66ba91f2e086 Mon Sep 17 00:00:00 2001 From: Simon G Date: Tue, 6 Apr 2021 14:01:15 +0200 Subject: [PATCH] - add ioc container and bootstrapper --- Mystify/App.axaml.cs | 17 +++++++++++++++-- Mystify/Bootstrapper.cs | 14 ++++++++++++++ Mystify/Mystify.csproj | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 Mystify/Bootstrapper.cs diff --git a/Mystify/App.axaml.cs b/Mystify/App.axaml.cs index c41cba2..b172692 100644 --- a/Mystify/App.axaml.cs +++ b/Mystify/App.axaml.cs @@ -1,6 +1,8 @@ using Avalonia; +using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using LightweightIocContainer.Interfaces; using Mystify.ViewModels; using Mystify.Views; @@ -8,19 +10,30 @@ namespace Mystify { public class App : Application { + private IIocContainer? _kernel; + public override void Initialize() => AvaloniaXamlLoader.Load(this); public override void OnFrameworkInitializationCompleted() { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + if (Design.IsDesignMode) + return; + + Bootstrapper bootstrapper = new(); + _kernel = bootstrapper.BootstrapKernel(); + + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopStyleApplicationLifetime) { - desktop.MainWindow = new MainWindow + desktopStyleApplicationLifetime.MainWindow = new MainWindow { DataContext = new MainWindowViewModel(), }; + desktopStyleApplicationLifetime.Exit += OnExit; } base.OnFrameworkInitializationCompleted(); } + + private void OnExit(object? sender, ControlledApplicationLifetimeExitEventArgs args) => _kernel?.Dispose(); } } \ No newline at end of file diff --git a/Mystify/Bootstrapper.cs b/Mystify/Bootstrapper.cs new file mode 100644 index 0000000..ad52015 --- /dev/null +++ b/Mystify/Bootstrapper.cs @@ -0,0 +1,14 @@ +// Author: Gockner, Simon +// Created: 2021-04-06 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using LightweightIocContainer; +using LightweightIocContainer.Interfaces; + +namespace Mystify +{ + public class Bootstrapper + { + public IIocContainer BootstrapKernel() => new IocContainer().Install(); + } +} \ No newline at end of file diff --git a/Mystify/Mystify.csproj b/Mystify/Mystify.csproj index 077a9a6..b2e7587 100644 --- a/Mystify/Mystify.csproj +++ b/Mystify/Mystify.csproj @@ -13,5 +13,6 @@ +