Extensions.Hosting.Avalonia 1.0.4

dotnet add package Extensions.Hosting.Avalonia --version 1.0.4
                    
NuGet\Install-Package Extensions.Hosting.Avalonia -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Extensions.Hosting.Avalonia" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Extensions.Hosting.Avalonia" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Extensions.Hosting.Avalonia" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Extensions.Hosting.Avalonia --version 1.0.4
                    
#r "nuget: Extensions.Hosting.Avalonia, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Extensions.Hosting.Avalonia@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Extensions.Hosting.Avalonia&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Extensions.Hosting.Avalonia&version=1.0.4
                    
Install as a Cake Tool

Avalonia.Extensions.Hosting

This is a port of nuitsjp's Wpf.Extensions.Hosting from WPF to Avalonia.

Working in progress, no available now.

Avalonia.Extensions.Hosting is a library for running Avalonia applications on Generic Host.

Many of the modern libraries in .NET are provided for Generic Hosts. By using this library, you can take advantage of the latest and greatest set of libraries provided for .

This library allows you to write Avalonia on Generic Host in a simple and intuitive way, just like .NET6.

// Create a builder by specifying the application and main window.
var builder = AvaloniaApplication<App, MainWindow>.CreateBuilder(args);

// Configure dependency injection.
// Injecting MainWindowViewModel into MainWindow.
builder.Services.AddTransient<MainWindowViewModel>();

// Configure the settings.
// Injecting IOptions<MySettings> from appsetting.json.
builder.Services.Configure<MySettings>(builder.Configuration.GetSection("MySettings"));

// Configure logging.
// Using the diagnostic logging library Serilog.
builder.Host.UseSerilog((hostingContext, services, loggerConfiguration) => loggerConfiguration
    .ReadFrom.Configuration(hostingContext.Configuration)
    .Enrich.FromLogContext()
    .WriteTo.Debug()
    .WriteTo.File(
        @"Logs\log.txt", 
        rollingInterval: RollingInterval.Day));
    
var app = builder.Build();

await app.RunAsync();

Getting Started

Create a Avalonia project and add the package from NuGet.

NuGet :Extensions.Hosting.Avalonia

Install-Package Extensions.Hosting.Avalonia

Since Avalonia is a reserved prefix for Avalonia, the package ID is not the same as the project name.

Stop the automatic generation of the application entry point (Main method). Open the .csproj file and set EnableDefaultApplicationDefinition to false.

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>WinExe</OutputType>
		<TargetFramework>net6.0-windows</TargetFramework>
		<Nullable>enable</Nullable>
		<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
		<ApplicationManifest>app.manifest</ApplicationManifest>
		<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Extensions.Hosting.Avalonia" Version="1.0.0" />
	</ItemGroup>

</Project>

Delete the description of StartupUri from App.xaml.

<Application x:Class="GettingStarted.App"
             xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:GettingStarted">
    <Application.Resources>
         
    </Application.Resources>
</Application>

Add a constructor to App.xaml.cs.

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }
    }

Create a Program.cs file and run the Avalonia application on the Generic Host.

using GettingStarted;

// Create a builder by specifying the application and main window.
var builder = AvaloniaApplication<App, MainWindow>.CreateBuilder(args);

// Build and run the application.
var app = builder.Build();
await app.RunAsync();

Use Dependency Injection.

The following is an example of injecting a ViewModel into MainWindow.

Create the MainWindowViewModel.

namespace GettingStarted;

public class MainWindowViewModel
{
    public string Message => "Hello, Generic Host!";
}

Modify the constructor of MainWindow to receive ViewModel as an argument of the constructor and set it to DataContext.

public MainWindow(MainWindowViewModel mainWindowViewModel)
{
    InitializeComponent();
    DataContext = mainWindowViewModel;
}

Register the ViewModel to the DI container in Program.cs.

// Register the ViewModel to be injected into MainWindow to the DI container.
builder.Services.AddTransient<MainWindowViewModel>();

In this way, all the features of Generic Host are available.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 136 12/27/2024
1.0.3 109 12/26/2024
1.0.1 109 12/23/2024
1.0.0 126 9/9/2024