-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example of a single root for Avalonia app
- Loading branch information
1 parent
ce8942f
commit c70367a
Showing
13 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
samples/SingleRootAvaloniaApp/SingleRootAvaloniaApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |