WPF Setup
1. Installation
Install MVVMToolkitExtensions.WPF through your method of choice:
.NET CLI:
dotnet add package MVVMToolkitExtensions.WPF
Package Manager:
Install-Package MVVMToolkitExtensions.WPF
2. Configuring Dependency Injection
MVVMToolkitExtensions is designed with IoC in mind and uses the reputable Microsoft.Extensions.DependencyInjection
library.
Instead of manually setting the DataContext
in each view, you can use a centralized approach:
App.xaml.cs:
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var services = new ServiceCollection(); // Register services and viewmodels services.AddViewModelMapping<MainWindow, MainWindowViewModel>(); services.AddViewModelMapping<ViewA, ViewAViewModel>(); // Show the main window services .BuildServiceProvider() .WithMainWindow<MainWindow>(); }