Wpf.Extensions.Hosting 1.2.0

dotnet add package Wpf.Extensions.Hosting --version 1.2.0
NuGet\Install-Package Wpf.Extensions.Hosting -Version 1.2.0
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="Wpf.Extensions.Hosting" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Wpf.Extensions.Hosting --version 1.2.0
#r "nuget: Wpf.Extensions.Hosting, 1.2.0"
#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.
// Install Wpf.Extensions.Hosting as a Cake Addin
#addin nuget:?package=Wpf.Extensions.Hosting&version=1.2.0

// Install Wpf.Extensions.Hosting as a Cake Tool
#tool nuget:?package=Wpf.Extensions.Hosting&version=1.2.0

Wpf.Extensions.Hosting

The Japanese document is here.

Wpf.Extensions.Hosting is a library for running WPF 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 WPF on Generic Host in a simple and intuitive way, just like .NET6.

// Create a builder by specifying the application and main window.
var builder = WpfApplication<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 WPF project and add the package from NuGet.

NuGet :Wpf.Extensions.Hosting

Install-Package Wpf.Extensions.Hosting

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>
		<UseWPF>true</UseWPF>
		<EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Wpf.Extensions.Hosting" Version="0.0.3" />
	</ItemGroup>

</Project>

Delete the description of StartupUri from App.xaml.

<Application x:Class="GettingStarted.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             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 WPF application on the Generic Host.

using GettingStarted;

// Create a builder by specifying the application and main window.
var builder = WpfApplication<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.

.NET Foundation

This project is part of the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Wpf.Extensions.Hosting:

Package Downloads
Kamishibai.Hosting

MVVM navigation library for Generic Host.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 501 2/2/2024
1.1.2 1,198 10/14/2022
1.1.0 834 4/6/2022
1.0.0 438 1/22/2022
0.0.3 452 1/21/2022