Nito.Hosting.Wpf
1.0.0-alpha.1
Prefix Reserved
dotnet add package Nito.Hosting.Wpf --version 1.0.0-alpha.1
NuGet\Install-Package Nito.Hosting.Wpf -Version 1.0.0-alpha.1
<PackageReference Include="Nito.Hosting.Wpf" Version="1.0.0-alpha.1" />
paket add Nito.Hosting.Wpf --version 1.0.0-alpha.1
#r "nuget: Nito.Hosting.Wpf, 1.0.0-alpha.1"
// 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
Hosting
.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 | Versions 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. |
-
.NETFramework 4.7.2
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
-
net6.0-windows7.0
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
-
net7.0-windows7.0
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
-
net8.0-windows7.0
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
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 |