Skip to content

Commit 54f2061

Browse files
Copilottautcony
andcommitted
Initial Avalonia project structure and core library setup
Co-authored-by: tautcony <8295052+tautcony@users.noreply.github.com>
1 parent 5e9030e commit 54f2061

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+7541
-0
lines changed

ChapterTool.Avalonia/App.axaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Application xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
x:Class="ChapterTool.Avalonia.App"
4+
xmlns:local="using:ChapterTool.Avalonia"
5+
RequestedThemeVariant="Default">
6+
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
7+
8+
<Application.DataTemplates>
9+
<local:ViewLocator/>
10+
</Application.DataTemplates>
11+
12+
<Application.Styles>
13+
<FluentTheme />
14+
</Application.Styles>
15+
</Application>

ChapterTool.Avalonia/App.axaml.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Avalonia;
2+
using Avalonia.Controls.ApplicationLifetimes;
3+
using Avalonia.Data.Core;
4+
using Avalonia.Data.Core.Plugins;
5+
using System.Linq;
6+
using Avalonia.Markup.Xaml;
7+
using ChapterTool.Avalonia.ViewModels;
8+
using ChapterTool.Avalonia.Views;
9+
10+
namespace ChapterTool.Avalonia;
11+
12+
public partial class App : Application
13+
{
14+
public override void Initialize()
15+
{
16+
AvaloniaXamlLoader.Load(this);
17+
}
18+
19+
public override void OnFrameworkInitializationCompleted()
20+
{
21+
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
22+
{
23+
// Avoid duplicate validations from both Avalonia and the CommunityToolkit.
24+
// More info: https://docs.avaloniaui.net/docs/guides/development-guides/data-validation#manage-validationplugins
25+
DisableAvaloniaDataAnnotationValidation();
26+
desktop.MainWindow = new MainWindow
27+
{
28+
DataContext = new MainWindowViewModel(),
29+
};
30+
}
31+
32+
base.OnFrameworkInitializationCompleted();
33+
}
34+
35+
private void DisableAvaloniaDataAnnotationValidation()
36+
{
37+
// Get an array of plugins to remove
38+
var dataValidationPluginsToRemove =
39+
BindingPlugins.DataValidators.OfType<DataAnnotationsValidationPlugin>().ToArray();
40+
41+
// remove each entry found
42+
foreach (var plugin in dataValidationPluginsToRemove)
43+
{
44+
BindingPlugins.DataValidators.Remove(plugin);
45+
}
46+
}
47+
}
172 KB
Binary file not shown.
30.5 KB
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>WinExe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
7+
<ApplicationManifest>app.manifest</ApplicationManifest>
8+
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
9+
<RootNamespace>ChapterTool</RootNamespace>
10+
<AssemblyName>ChapterTool</AssemblyName>
11+
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<Folder Include="Models\" />
16+
<AvaloniaResource Include="Assets\**" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="Avalonia" Version="11.3.6" />
21+
<PackageReference Include="Avalonia.Desktop" Version="11.3.6" />
22+
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.6" />
23+
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.6" />
24+
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
25+
<PackageReference Include="Avalonia.Diagnostics" Version="11.3.6">
26+
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
27+
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
28+
</PackageReference>
29+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
30+
</ItemGroup>
31+
</Project>

ChapterTool.Avalonia/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Avalonia;
2+
using System;
3+
4+
namespace ChapterTool.Avalonia;
5+
6+
sealed class Program
7+
{
8+
// Initialization code. Don't use any Avalonia, third-party APIs or any
9+
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
10+
// yet and stuff might break.
11+
[STAThread]
12+
public static void Main(string[] args) => BuildAvaloniaApp()
13+
.StartWithClassicDesktopLifetime(args);
14+
15+
// Avalonia configuration, don't remove; also used by visual designer.
16+
public static AppBuilder BuildAvaloniaApp()
17+
=> AppBuilder.Configure<App>()
18+
.UsePlatformDetect()
19+
.WithInterFont()
20+
.LogToTrace();
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using Avalonia.Controls;
3+
using Avalonia.Controls.Templates;
4+
using ChapterTool.Avalonia.ViewModels;
5+
6+
namespace ChapterTool.Avalonia;
7+
8+
public class ViewLocator : IDataTemplate
9+
{
10+
11+
public Control? Build(object? param)
12+
{
13+
if (param is null)
14+
return null;
15+
16+
var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);
17+
var type = Type.GetType(name);
18+
19+
if (type != null)
20+
{
21+
return (Control)Activator.CreateInstance(type)!;
22+
}
23+
24+
return new TextBlock { Text = "Not Found: " + name };
25+
}
26+
27+
public bool Match(object? data)
28+
{
29+
return data is ViewModelBase;
30+
}
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ChapterTool.Avalonia.ViewModels;
2+
3+
public partial class MainWindowViewModel : ViewModelBase
4+
{
5+
public string Greeting { get; } = "Welcome to Avalonia!";
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
3+
namespace ChapterTool.Avalonia.ViewModels;
4+
5+
public class ViewModelBase : ObservableObject
6+
{
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Window xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:vm="using:ChapterTool.Avalonia.ViewModels"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
7+
x:Class="ChapterTool.Avalonia.Views.MainWindow"
8+
x:DataType="vm:MainWindowViewModel"
9+
Icon="/Assets/avalonia-logo.ico"
10+
Title="ChapterTool.Avalonia">
11+
12+
<Design.DataContext>
13+
<!-- This only sets the DataContext for the previewer in an IDE,
14+
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
15+
<vm:MainWindowViewModel/>
16+
</Design.DataContext>
17+
18+
<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
19+
20+
</Window>

0 commit comments

Comments
 (0)