Nito.Hosting.Wpf 1.0.0-alpha.1

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

// Install Nito.Hosting.Wpf as a Cake Tool
#tool nuget:?package=Nito.Hosting.Wpf&version=1.0.0-alpha.1&prerelease                

Logo

Hosting Build status codecov NuGet version API docs

.NET Generic Host support for UI applications.

Usage (WPF)

Afer installing the NuGet package, add a Main method to your application component (commonly called App.xaml):

// Note: [STAThread] is not used!
private static void Main()
{
	var hostBuilder = Host.CreateApplicationBuilder();
	hostBuilder.Services.AddWpfApplication<App>();
	var host = hostBuilder.Build();
	host.Run();
}

Next, disable the WPF-provided Main method.

For SDK-style WPF apps, set EnableDefaultApplicationDefinition to false in your project's properties.

For .NET Framework WPF apps, change the build tool for your application component (usually App.xaml) from Application to Page.

The .NET Generic Host and WPF Lifetime

The .NET Generic Host will shut down when the WPF application exits. Also, when the .NET Generic Host shuts down, it will request the WPF application to exit.

So, your application can exit the WPF application normally (e.g., Application.Current.Shutdown()), or it can request a shutdown via the .NET Generic Host (e.g., IHostApplicationLifetime.StopApplication()). Either of these will shut down the application cleanly.

Using the .NET Generic Host

You can modify the Main method to:

  • Register additional services for DI.
  • Save the host and/or its IServiceProvider to act as a service locator.

This project provides nothing fancy like automatically injecting ViewModels into Views, or Views for ViewModels. The exact way you use DI is up to you; this project just provides the .NET Generic Host with a lifetime compatible with UI applications.

Getting the Main Thread and its Dispatcher

AddWpfApplication<App> registers App as a singleton.

After the host has started, you can get to the main thread's dispatcher from that:

Dispatcher dispatcher = host.Services.GetRequiredService<App>().Dispatcher;

Similarly, you can get to the main UI thread from that dispatcher:

Thread thread = host.Services.GetRequiredService<App>().Dispatcher.Thread;

Be sure not to call GetRequiredService<App>() until after the host has started. Otherwise, the App instance will be created on the wrong thread and Bad Things will happen.

Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net8.0-windows was computed.  net8.0-windows7.0 is compatible. 
.NET Framework net472 is compatible.  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

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.0-alpha.1 95 1/30/2024