Extensions.Hosting.Wpf 3.4.0

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

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

Microsoft.Extensions.Hosting.Wpf

Nuget Nuget GitHub

Unofficial implementation of Microsoft.Extensions.Hosting for WPF. It is inspired by Dapplo and this extensions is focused only on WPF and doesn't have Plugins, SingleInstance etc features like Dapplo. It's main feature is to provide the ability to bind DataContext with ViewModels directly in XAML where the ViewModel gets resolved by DI. This library also has few extensions packages to add features like tray icon, thread swithcing between main thread and threadpool thread, 3rd party DI support.

Changelog

Samples

  1. HostingSimple: This project serves as a minimalistic, beginner-friendly introduction. It offers a basic starting point for understanding the Microsoft.Extensions.Hosting.Wpf framework.
  2. HostingReactiveUI: In this more advanced example, NLog is utilized for logging, and ReactiveUI is employed as the model-view-viewmodel framework. Additionally, the example showcases the integration of the TrayIcon feature.
  3. HostingReactiveUISimpleInjector: Similar to HostingReactiveUI, this project not only employs ReactiveUI but also incorporates SimpleInjector. This library doesn't limits your to stick only with Microsoft.DependencyInjection. This example further provides insights into additional abstractions and internal utilities for managing different DI frameworks.
  4. HostingReactiveUISimpleInjectorAmbientScope: Building upon the concepts of HostingReactiveUISimpleInjector, this project delves into the application of the AsyncScopedLifestyle with SimpleInjector. This advanced example illustrates the support for ambient-scoping, as described in The Ambient Composition Model. Moreover, it demonstrates the integration of Microsoft.Extensions.Hosting.Wpf.Threading.
  5. HostingReactiveUISimpleInjectorFlowingScope: Building upon the concepts of HostingReactiveUISimpleInjector, this project delves into the application of the ScopedLifestyle.Flowing with SimpleInjector (similar mechanisms that is used by Microsoft DI). This advanced example illustrates the support for closure-scoping, as described in The Closure Composition Model. This sample doesn't use the ViewModelLocatorHost, presenting an alternative approach for leveraging IViewModelLocator to inject view models for views. It also showcases the integration of Microsoft.Extensions.Hosting.Wpf.Threading.

Getting Started

This steps including the Locator feature for Views. If you don't want it then just skip to 6 and 7 step.

In fact, it has alternative method to inject ViewModels to View. Usually used when you need to use closure-scoping with DI. Please, refer to HostingReactiveUISimpleInjectorFlowingScope sample for such scenario.

1. First step, make IViewModelLocator that will contain your ViewModels. Example:

public interface IViewModelLocator
{
    MainViewModel Main { get; }

    ChildViewModel Child { get; }
}

2. Make implementation for IViewModelLocator. This example is using IServiceProvider

public class ViewModelLocator : IViewModelLocator
{
    public IServiceProvider Container { get; }

    public MainViewModel Main => Container.GetRequiredService<MainViewModel>();

    public ChildViewModel Child => Container.GetRequiredService<ChildViewModel>();

    public ViewModelLocator(IServiceProvider container)
    {
        Container = container;
    }
}

3. Add ViewModelLocatorHost that inherits AbstractViewModelLocatorHost<IViewModelLocator>

public class ViewModelLocatorHost : AbstractViewModelLocatorHost<IViewModelLocator>
{
}

4. In your App.xaml add Locator resource

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            
        </ResourceDictionary.MergedDictionaries>
        <locator:ViewModelLocatorHost x:Key="Locator"/>
    </ResourceDictionary>
</Application.Resources>

5. Add in App.xaml.cs two interfaces IViewModelLocatorInitialization<ViewModelLocator> and IApplicationInitializeComponent

public partial class App : Application, IViewModelLocatorInitialization<IViewModelLocator>, IApplicationInitializeComponent
{
    public void Initialize()
    {
        //Here we can initialize important things. This method always runs on UI thread. 
        //In this example it's empty as we do not have anything to initialize like ReactiveUI
    }
	
    public void InitializeLocator(IViewModelLocator viewModelLocator)
    {
        //Runs after Initialize method.
        //We need to set it so that our <locator:ViewModelLocatorHost x:Key="Locator"/> could resolve ViewModels for DataContext
        //You can also use it as service locator pattern, but I personally recommend you to use it only inside View xaml to bind the DataContext
        var viewModelLocatorHost = ViewModelLocatorHost.GetInstance(this);
        viewModelLocatorHost.SetViewModelLocator(viewModelLocator);
    }
}

NB! ViewModelLocatorHost.GetInstance(this) will automatically find the locator even if you rename it(x:Key) in App.xaml, but for better perfomance, startup time, memory usage(it will iterate through Application Dictionary) my personal recommendation is to use ViewModelLocatorHost.GetInstance(this, "Locator") instead.

6. Add Program.cs. Basic example

public class Program
{
	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	public static void Main(string[] args)
	{
		using IHost host = CreateHostBuilder(args)
			.Build()
			.UseWpfViewModelLocator<App, IViewModelLocator>(provider => new ViewModelLocator(provider));
		host.Run();
	}

	private static IHostBuilder CreateHostBuilder(string[] args)
	{
		return Host.CreateDefaultBuilder(args)
			.ConfigureServices(ConfigureServices);
	}

	private static void ConfigureServices(HostBuilderContext hostContext, IServiceCollection services)
	{
		services.AddLogging();
		services.AddWpf<App>();

		//Add our view models
		services.AddTransient<MainViewModel>();
		services.AddTransient<ChildViewModel>();
	}
}

7. We need to add StartupObject in our .csproj

<StartupObject>[Namespace].Program</StartupObject>

8. Now in your View you can bind the DataContext like this

DataContext="{Binding ViewModelLocator.Main, Mode=OneTime, Source={StaticResource Locator}}"

WPF Lifetime

If you want you can use UseWpfLifetime but it's pretty much experimental, the current solution is well adopted to use the default lifetime and was battle tested without UseWpfLifetime.

Usage

private static IHostBuilder CreateHostBuilder(string[] args)
{
	return Host.CreateDefaultBuilder(args)
		.UseWpfLifetime() //<-- new line
		.ConfigureServices(ConfigureServices);
}

Other features

  1. Microsoft.Extensions.Hosting.Wpf.Threading
  2. Microsoft.Extensions.Hosting.Wpf.Bootstrap
  3. Microsoft.Extensions.Hosting.Wpf.TrayIcon
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net6.0-windows7.0 is compatible.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net8.0 was computed.  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.  net8.0-windows7.0 is compatible. 
.NET Core netcoreapp3.1 is compatible. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

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

Package Downloads
Extensions.Hosting.Wpf.Threading

Adds Microsoft.VisualStudio.Threading support for Extensions.Hosting.Wpf

Extensions.Hosting.Wpf.Bootstrap

A small helper to bootstrap 3rd party DI containers for Extensions.Hosting.Wpf

Extensions.Hosting.Wpf.TrayIcon

Adds tray icon for Extensions.Hosting.Wpf

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.4.0 377 11/23/2023
3.3.0 608 3/8/2023
3.2.0 509 2/7/2023
3.1.0 569 1/26/2023
3.0.0 1,013 6/24/2022
2.0.0 787 6/8/2022
1.0.0 615 6/5/2022