A simple .NET library enabling MVVM, DI and appsettings.conf in WPF projects Uses Microsoft.Toolkit.Mvvm that lacks official documentation for WPF applications. This library aims to make creating simple WPF applications easier and faster.
- Introduces MVVM pattern to .NET5/6 WPF applications
- Enables dependency injection from Microsoft.Extensions.DependencyInjection
- Introduces appsettings.conf configuration file known from ASP.NET Core projects
Download package from nuget repository:
Install-Package WPF.Core
or
dotnet add package WPF.Core
- All view model classes should inherit 'ViewModel' class:
internal class Module1ViewModel : ViewModel {}
- View xaml.cs files should set context to correct view model class:
DataContext = Ioc.Default.GetService<Module1ViewModel>();
- Configure App.xaml.cs
protected override void OnStartup(StartupEventArgs args)
{
base.OnStartup(args);
new ServiceCollectionBuilder()
.AddMainWindow<MainWindow>() // class of main window
.ConfigureServices(services =>
{
services.AddTransient<IInjectionService, InjectionService>(); // services to register, other than view models
})
.AddConfiguration() // add support for appsettings.json file
.AddViewModels() // registers all classes inheriting ViewModel class
.Build();
Ioc.Default.GetService<MainWindow>()!.Show(); // display main program window
}
If you register and show main window using the above code, you should remove following line from App.xaml file StartupUri="MainWindow.xaml"