My project is built with Catel.MVVM
, but I am very open to any solution for WPF MVVM with DI that is not Catel.MVVM
specific to solve my problem. The project is .NET 5.
I am struggling to get a solution for DI injecting dependencies into ViewModels with Catel.MVVM
. I'm using the .NET Core implementation of DI.
What I want:
- Have DI inject dependencies into ViewModels when showing Views. Right now without a parameter-less constructor the ViewModel throws an exception due to the DI not creating the ViewModel.
- Have Catel.MVVM auto associate the ViewModel for each View so that it does not need to be set in XAML.
I know this is possible with Catel.MVVM
because I see it working in the WildGums project LogViewer, but I am having trouble wrapping my head around how it works. I have also browsed through each WPF project in Catel.Examples. I'm either overlooking something or just do not understand how WPF MVVM with DI works. This is my first time using DI with MVVM.
I thought I would be lucky and this would be handled automatically if I use the naming conventions described in the docs.
I have a project with Catel.MVVM & working DI with the exception of ViewModel injection, CatelMvvmDI. As the project sits, ViewModel association is handled in XAML.
Register the View and ViewModel:
var viewModelLocator = _host.Services.GetRequiredService<IViewModelLocator>();
viewModelLocator.Register<MainView, MainViewModel>();
Show MainView:
var main = _host.Services.GetRequiredService<MainView>();
main.Show();
The correct view model type is resolved:
var viewModelLocator = _host.Services.GetRequiredService<IViewModelLocator>();
var viewModelType = viewModelLocator.ResolveViewModel(typeof(MainView));
This might be where I'm going wrong when registering the View & ViewModel:
services.AddSingleton<MainView>();
services.AddTransient<MainViewModel>();