Skip to content

Commit

Permalink
Add an example of a single root for Avalonia app
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed May 21, 2024
1 parent ce8942f commit c70367a
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Pure.DI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeatherForecast", "samples\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalWebAPI", "samples\MinimalWebAPI\MinimalWebAPI.csproj", "{60F18CFA-957B-488F-8292-467D92C17267}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SingleRootAvaloniaApp", "samples\SingleRootAvaloniaApp\SingleRootAvaloniaApp.csproj", "{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -200,6 +202,10 @@ Global
{60F18CFA-957B-488F-8292-467D92C17267}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60F18CFA-957B-488F-8292-467D92C17267}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60F18CFA-957B-488F-8292-467D92C17267}.Release|Any CPU.Build.0 = Release|Any CPU
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7C9E056B-CBA9-4548-9CDB-C5CE03C491B0} = {8163CDD7-7018-4301-A984-803C3807A6A6}
Expand All @@ -225,5 +231,6 @@ Global
{D1C043C4-ED8F-45C5-8077-EF10AA71B951} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{9A3E2271-3090-4BCE-BB48-6C0724CFBE44} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{60F18CFA-957B-488F-8292-467D92C17267} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8} = {FA80D231-C641-4A49-99C6-0C065D818B07}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions samples/Clock/Clock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<InternalsVisibleTo Include="WinFormsApp" />
<InternalsVisibleTo Include="WinFormsAppNetCore" />
<InternalsVisibleTo Include="AvaloniaApp" />
<InternalsVisibleTo Include="SingleRootAvaloniaApp" />
<InternalsVisibleTo Include="BlazorWebAssemblyApp" />
<InternalsVisibleTo Include="BlazorServerApp" />
<InternalsVisibleTo Include="MAUIApp" />
Expand Down
13 changes: 13 additions & 0 deletions samples/SingleRootAvaloniaApp/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApp.App"
xmlns:local="using:AvaloniaApp"
RequestedThemeVariant="Default">
<Application.Resources>
<local:Composition x:Key="Composition" />
</Application.Resources>
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
24 changes: 24 additions & 0 deletions samples/SingleRootAvaloniaApp/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

namespace AvaloniaApp;

public class App : Application
{
public override void Initialize() => AvaloniaXamlLoader.Load(this);

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop
&& Resources["Composition"] is Composition composition)
{
// Assignment of the main window
desktop.MainWindow = composition.App.MainWindow;
// Handles disposables
desktop.Exit += (_, _) => composition.Dispose();
}

base.OnFrameworkInitializationCompleted();
}
}
13 changes: 13 additions & 0 deletions samples/SingleRootAvaloniaApp/AppDataContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace AvaloniaApp;

using Clock.ViewModels;
using Views;

internal class AppDataContext(
Lazy<MainWindow> mainWindow,
IClockViewModel clockViewModel)
{
public MainWindow MainWindow => mainWindow.Value;

public IClockViewModel ClockViewModel => clockViewModel;
}
Binary file not shown.
31 changes: 31 additions & 0 deletions samples/SingleRootAvaloniaApp/Composition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedMember.Global
// ReSharper disable RedundantNameQualifier
// ReSharper disable ArrangeTypeMemberModifiers
namespace AvaloniaApp;

using Clock.Models;
using Clock.ViewModels;
using Pure.DI;
using static Pure.DI.Lifetime;

internal partial class Composition
{
void Setup() => DI.Setup(nameof(Composition))
// A single compositional root for the application
.Root<AppDataContext>("App")

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

// View Models
.Bind().To<ClockViewModel>()

// Models
.Bind().To<Log<TT>>()
.Bind().To(_ => TimeSpan.FromSeconds(1))
.Bind().As(Singleton).To<Clock.Models.Timer>()
.Bind().As(PerBlock).To<SystemClock>()

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

using Clock.ViewModels;

// ReSharper disable once ClassNeverInstantiated.Global
internal class Dispatcher: IDispatcher
{
public void Dispatch(Action action) =>
Avalonia.Threading.Dispatcher.UIThread.Post(action);
}
21 changes: 21 additions & 0 deletions samples/SingleRootAvaloniaApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Avalonia;
// ReSharper disable ClassNeverInstantiated.Global

namespace AvaloniaApp;

public class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
private static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}
26 changes: 26 additions & 0 deletions samples/SingleRootAvaloniaApp/SingleRootAvaloniaApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(BaseTargetFramework)</TargetFramework>
<OutputType>WinExe</OutputType>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
<NoWarn>NU1801</NoWarn>
<RootNamespace>AvaloniaApp</RootNamespace>
</PropertyGroup>

<ItemGroup>
<AvaloniaResource Include="Assets\**"/>
<ProjectReference Include="..\..\src\Pure.DI.Core\Pure.DI.Core.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\..\src\Pure.DI\Pure.DI.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\Clock\Clock.csproj"/>
<PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.10" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.10" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.10" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.10" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions samples/SingleRootAvaloniaApp/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:avaloniaApp="clr-namespace:AvaloniaApp"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaApp.Views.MainWindow"
x:DataType="avaloniaApp:Composition"
DataContext="{StaticResource Composition}"
Title="{Binding App.ClockViewModel.Time}"
Icon="/Assets/avalonia-logo.ico"
FontFamily="Consolas"
FontWeight="Bold">

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" DataContext="{Binding App.ClockViewModel}">
<TextBlock Text="{Binding Date}" FontSize="64" HorizontalAlignment="Center" />
<TextBlock Text="{Binding Time}" FontSize="128" HorizontalAlignment="Center" />
</StackPanel>

</Window>
9 changes: 9 additions & 0 deletions samples/SingleRootAvaloniaApp/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Avalonia.Controls;

namespace AvaloniaApp.Views;

// ReSharper disable once ClassNeverInstantiated.Global
public partial class MainWindow : Window
{
public MainWindow() => InitializeComponent();
}
18 changes: 18 additions & 0 deletions samples/SingleRootAvaloniaApp/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embeded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="SingleRootAvaloniaApp.Desktop"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>

0 comments on commit c70367a

Please sign in to comment.