Dependency Injection returning null value
I've created a WPF application that uses DI for the View/Viewmodel. I'm attempting to create a CS file to deal with the program logic that uses a dependency injected Service Wrapper. However, whenever I call the Service in the new class, the value is returning as null. I've included a shortened version below that hopefully gets the point across.
MyService.CS MainWindow.xaml.cs MainWindow.Xaml MainViewModel.cs App.xaml App.Xaml.Cs There is a lot more there, but they are all instantiated as singleton. HomeView.xaml.Cs And finally. Logic.CS
MyService.CS MainWindow.xaml.cs MainWindow.Xaml MainViewModel.cs App.xaml App.Xaml.Cs There is a lot more there, but they are all instantiated as singleton. HomeView.xaml.Cs And finally. Logic.CS
10 Replies
I dont see where you add the services to a serviceprovider and then build the provider?
I skipped adding that since I was copying this from a computer without discord access
let me edit that in from what it really looks like
The DI was working as expected (allowing me to change view/viewmodels with the Navigate commands) before I tried to add Logic.CS into the mix
in the MainViewModel, is CustomWrapper null?
Well that's weird - it is null
that tells me the DI isnt setup correctly
Let me see if it returns null before I change ViewModels
When I breakpoint NavigateHomeCommand, customWrapper shows Namespace.Services.CustomWrapper so it's set then
so where does customWrapper show null?
I think I just figured it out
I shortened the example for the 2000/4000 character limit, but in actuality the proceed button goes to HomeView MOdel which I then select the next ViewModel which we can call AwayViewModel
But I don't have the DI setup on the first ViewModel I change to so I think it's nulling out since that ViewModel isn't setup with DI but the other one is
ahhhhh
So the MainViewModel files above are actually AwayViewModel in my actual program
But it makes sense since I don't have that setup
I will jump immediately to AwayViewModel and see if it nulls but I think it should work
Ahh yes that's my problem thank you!
I set up the DI on the MainViewModel, but not the subsequent Away/Home view models
So when I swithc VM, it's dropping the MainViewModel as expected but I just set it up on the other 2 and it'll carry