Skip to content

Commit

Permalink
Improved Avalonia examples
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 15, 2024
1 parent 401b2f5 commit 0cfb411
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
22 changes: 17 additions & 5 deletions readme/Avalonia.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 17 additions & 5 deletions readme/AvaloniaPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 17 additions & 5 deletions samples/AvaloniaApp/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 23 additions & 5 deletions samples/SingleRootAvaloniaApp/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions samples/SingleRootAvaloniaApp/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal partial class Composition
void Setup() => DI.Setup()
// Single composition root for the application
.Root<Root>(nameof(Root))

.Bind().As(Singleton).To<Root>()

// View Models
Expand All @@ -25,7 +25,7 @@ void Setup() => DI.Setup()
.Bind().To(_ => TimeSpan.FromSeconds(1))
.Bind().As(Singleton).To<Clock.Models.Timer>()
.Bind().As(PerBlock).To<SystemClock>()

// Infrastructure
.Bind().To<Dispatcher>();
}
6 changes: 4 additions & 2 deletions samples/SingleRootAvaloniaApp/Root.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace AvaloniaApp;

using Clock.ViewModels;
using Pure.DI;
using Views;

internal record Root(
Lazy<MainWindow> MainWindow,
internal readonly record struct Root(
IOwned Owned,
Func<MainWindow> CreateMainWindow,
IClockViewModel ClockViewModel);

0 comments on commit 0cfb411

Please sign in to comment.