From 0cfb411f274a2c2075f7b3b02d680f8787dde058 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Thu, 15 Aug 2024 11:47:40 +0300 Subject: [PATCH] Improved Avalonia examples --- readme/Avalonia.md | 22 +++++++++++---- readme/AvaloniaPageTemplate.md | 22 +++++++++++---- samples/AvaloniaApp/App.axaml.cs | 22 +++++++++++---- samples/SingleRootAvaloniaApp/App.axaml.cs | 28 ++++++++++++++++---- samples/SingleRootAvaloniaApp/Composition.cs | 4 +-- samples/SingleRootAvaloniaApp/Root.cs | 6 +++-- 6 files changed, 80 insertions(+), 24 deletions(-) diff --git a/readme/Avalonia.md b/readme/Avalonia.md index 34f4225c4..6db52efbc 100644 --- a/readme/Avalonia.md +++ b/readme/Avalonia.md @@ -59,13 +59,25 @@ public class App : Application public override void OnFrameworkInitializationCompleted() { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop - && Resources["Composition"] is Composition composition) + if (Resources[nameof(Composition)] is Composition composition) { - // Assignment of the main window - desktop.MainWindow = composition.MainWindow; + // Assigns the main window/view + switch (ApplicationLifetime) + { + case IClassicDesktopStyleApplicationLifetime desktop: + desktop.MainWindow = composition.MainWindow; + break; + + case ISingleViewApplicationLifetime singleViewPlatform: + singleViewPlatform.MainView = composition.MainWindow; + break; + } + // Handles disposables - desktop.Exit += (_, _) => composition.Dispose(); + if (ApplicationLifetime is IControlledApplicationLifetime controlledApplicationLifetime) + { + controlledApplicationLifetime.Exit += (_, _) => composition.Dispose(); + } } base.OnFrameworkInitializationCompleted(); diff --git a/readme/AvaloniaPageTemplate.md b/readme/AvaloniaPageTemplate.md index 9c682ac87..2f2d5bfb1 100644 --- a/readme/AvaloniaPageTemplate.md +++ b/readme/AvaloniaPageTemplate.md @@ -59,13 +59,25 @@ public class App : Application public override void OnFrameworkInitializationCompleted() { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop - && Resources["Composition"] is Composition composition) + if (Resources[nameof(Composition)] is Composition composition) { - // Assignment of the main window - desktop.MainWindow = composition.MainWindow; + // Assigns the main window/view + switch (ApplicationLifetime) + { + case IClassicDesktopStyleApplicationLifetime desktop: + desktop.MainWindow = composition.MainWindow; + break; + + case ISingleViewApplicationLifetime singleViewPlatform: + singleViewPlatform.MainView = composition.MainWindow; + break; + } + // Handles disposables - desktop.Exit += (_, _) => composition.Dispose(); + if (ApplicationLifetime is IControlledApplicationLifetime controlledApplicationLifetime) + { + controlledApplicationLifetime.Exit += (_, _) => composition.Dispose(); + } } base.OnFrameworkInitializationCompleted(); diff --git a/samples/AvaloniaApp/App.axaml.cs b/samples/AvaloniaApp/App.axaml.cs index fcf934ad3..a1adcb060 100644 --- a/samples/AvaloniaApp/App.axaml.cs +++ b/samples/AvaloniaApp/App.axaml.cs @@ -10,13 +10,25 @@ public class App : Application public override void OnFrameworkInitializationCompleted() { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop - && Resources["Composition"] is Composition composition) + if (Resources[nameof(Composition)] is Composition composition) { - // Assignment of the main window - desktop.MainWindow = composition.MainWindow; + // Assigns the main window/view + switch (ApplicationLifetime) + { + case IClassicDesktopStyleApplicationLifetime desktop: + desktop.MainWindow = composition.MainWindow; + break; + + case ISingleViewApplicationLifetime singleViewPlatform: + singleViewPlatform.MainView = composition.MainWindow; + break; + } + // Handles disposables - desktop.Exit += (_, _) => composition.Dispose(); + if (ApplicationLifetime is IControlledApplicationLifetime controlledApplicationLifetime) + { + controlledApplicationLifetime.Exit += (_, _) => composition.Dispose(); + } } base.OnFrameworkInitializationCompleted(); diff --git a/samples/SingleRootAvaloniaApp/App.axaml.cs b/samples/SingleRootAvaloniaApp/App.axaml.cs index c306902d4..0edd2319d 100644 --- a/samples/SingleRootAvaloniaApp/App.axaml.cs +++ b/samples/SingleRootAvaloniaApp/App.axaml.cs @@ -10,13 +10,31 @@ public class App : Application public override void OnFrameworkInitializationCompleted() { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop - && Resources["Composition"] is Composition composition) + if (Resources[nameof(Composition)] is Composition composition) { - // Assignment of the main window - desktop.MainWindow = composition.Root.MainWindow.Value; + // Assigns the main window/view + switch (ApplicationLifetime) + { + case IClassicDesktopStyleApplicationLifetime desktop: + desktop.MainWindow = composition.Root.CreateMainWindow(); + break; + + case ISingleViewApplicationLifetime singleViewPlatform: + singleViewPlatform.MainView = composition.Root.CreateMainWindow(); + break; + } + // Handles disposables - desktop.Exit += (_, _) => composition.Dispose(); + if (ApplicationLifetime is IControlledApplicationLifetime controlledApplicationLifetime) + { + controlledApplicationLifetime.Exit += (_, _) => + { + // Disposal of root objects with lifetime Transient, PerBlock, PerResolve + composition.Root.Owned.Dispose(); + // Dispose of singletons + composition.Dispose(); + }; + } } base.OnFrameworkInitializationCompleted(); diff --git a/samples/SingleRootAvaloniaApp/Composition.cs b/samples/SingleRootAvaloniaApp/Composition.cs index a47c186c4..ab2d34184 100644 --- a/samples/SingleRootAvaloniaApp/Composition.cs +++ b/samples/SingleRootAvaloniaApp/Composition.cs @@ -14,7 +14,7 @@ internal partial class Composition void Setup() => DI.Setup() // Single composition root for the application .Root(nameof(Root)) - + .Bind().As(Singleton).To() // View Models @@ -25,7 +25,7 @@ void Setup() => DI.Setup() .Bind().To(_ => TimeSpan.FromSeconds(1)) .Bind().As(Singleton).To() .Bind().As(PerBlock).To() - + // Infrastructure .Bind().To(); } \ No newline at end of file diff --git a/samples/SingleRootAvaloniaApp/Root.cs b/samples/SingleRootAvaloniaApp/Root.cs index 7af65cbe8..72692db39 100644 --- a/samples/SingleRootAvaloniaApp/Root.cs +++ b/samples/SingleRootAvaloniaApp/Root.cs @@ -1,8 +1,10 @@ namespace AvaloniaApp; using Clock.ViewModels; +using Pure.DI; using Views; -internal record Root( - Lazy MainWindow, +internal readonly record struct Root( + IOwned Owned, + Func CreateMainWindow, IClockViewModel ClockViewModel); \ No newline at end of file