Extensions.Hosting.Wpf
1.0.0
See the version list below for details.
dotnet add package Extensions.Hosting.Wpf --version 1.0.0
NuGet\Install-Package Extensions.Hosting.Wpf -Version 1.0.0
<PackageReference Include="Extensions.Hosting.Wpf" Version="1.0.0" />
paket add Extensions.Hosting.Wpf --version 1.0.0
#r "nuget: Extensions.Hosting.Wpf, 1.0.0"
// Install Extensions.Hosting.Wpf as a Cake Addin #addin nuget:?package=Extensions.Hosting.Wpf&version=1.0.0 // Install Extensions.Hosting.Wpf as a Cake Tool #tool nuget:?package=Extensions.Hosting.Wpf&version=1.0.0
Microsoft.Extensions.Hosting.Wpf
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. The second feature is the ability to use TrayIcon with this library because with Microsoft.Extensions.Hostin it's tricky.
This is more or less how I see Microsoft would do it for WPF without changing the WPF codedbase.
Samples
- HostingSimple - Minimal getting started project.
- HostingReactiveUI - More advanced example with using NLog as logging, ReactiveUI as model-view-viewmodel framework, shows how to use the TrayIcon feature.
- HostingReactiveUISimpleInjector - Same as HostingReactiveUI but it also using SimpleInjector. This library doesn't limits your to stick only with
Microsoft.DependencyInjection
. Also shows some more abstractions and internal helpers to handle another DI inside.
Getting Started
This steps including the Locator feature for Views. If you don't want it then just skip to 6 and 7 step.
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<ViewModelLocator>, IApplicationInitializeComponent
{
public void Initialize(ViewModelLocator viewModelLocator)
{
//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);
}
}
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, ViewModelLocator>(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>();
}
}
NB! AddWpf has an overload and you can for example add additional services to App
services.AddWpf(serviceProvider =>
{
var logger = serviceProvider.GetRequiredService<ILogger<App>>();
return new App(logger);
});
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}}"
Product | Versions 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. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.Extensions.Hosting (>= 5.0.0)
-
net6.0-windows7.0
- Microsoft.Extensions.Hosting (>= 5.0.0)
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.