Skip to content

ScarletKuro/Microsoft.Extensions.Hosting.Wpf

Repository files navigation

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 switching between main thread and threadpool thread, 3rd party DI support.

NuGet Packages

Name Description Latest version
Extensions.Hosting.Wpf The main library, with Microsoft.Extensions.Hosting for WPF support. NuGet
Extensions.Hosting.Wpf.TrayIcon An extension enabling tray icon support. NuGet
Extensions.Hosting.Wpf.Threading An extension enabling thread switching between Main UI Thread and ThreadPool Thread via Microsoft.VisualStudio.Threading. NuGet
Extensions.Hosting.Wpf.Bootstrap An extension enabling easier bootstrap for 3rd-party DI containers. NuGet

Quick Start

1. Add IApplicationInitializeComponent to App.xaml.cs

public partial class App : Application, IApplicationInitializeComponent
{
}

2. Create Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        using IHost host = CreateHostBuilder(args).Build();
        host.Run();
    }

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

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

3. Update .csproj

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

For the full guide including ViewModel Locator, constructor injection, and more, see the Wiki.

Samples

  1. HostingSimple: A minimalistic, beginner-friendly introduction. Offers a basic starting point for understanding the framework.
  2. HostingReactiveUI: An advanced example using NLog for logging, ReactiveUI as the MVVM framework, and the TrayIcon feature.
  3. HostingReactiveUISimpleInjector: Incorporates SimpleInjector alongside ReactiveUI. Demonstrates that you're not limited to Microsoft.DependencyInjection.
  4. HostingReactiveUISimpleInjectorAmbientScope: Demonstrates AsyncScopedLifestyle with SimpleInjector for ambient-scoping and integrates Threading.
  5. HostingReactiveUISimpleInjectorFlowingScope: Demonstrates ScopedLifestyle.Flowing with SimpleInjector for closure-scoping. Presents an alternative approach without ViewModelLocatorHost.

Documentation

Full documentation is available in the Wiki:

About

Unofficial implementation of Microsoft.Extensions.Hosting for WPF.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

 
 
 

Contributors