You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public static IServiceProvider ServiceProvider { get; private set; }
Access it via App class:
App.ServiceProvider.GetService<...>
havent tested if it works :D
That is a way to do it, but you're shooting yourself in the foot a little bit if you're doing anything serious. That's because instead of abstracting dependencies, you now have a dependency on that service provider.
It's a lot nicer when your actual UI parts like user controls don't need to know about services at all, rather they provide a rendering of the underlying data (or viewmodel if you're fancy). It's that underlying little bean that needs to know how to talk to services. But the actual wpf parts can remain blissfully unconcerned.
If the MainWindow constructor requires all of the services and factories needed to pass down to all the data parts, it can stay pretty clean.
Typically when you're doing it this way, though, the only parameter for MainWindow would be an IMainWindowViewModel or something similar and in the implementation of that is where it goes hog wild with dependency requirements.
The problem with constructor injection in WPF is that usercontrol hierachy and how one would normally write XAML, wont use the serviceprovider to fetch usercontrols.
Vehicles are generated on the fly by the XAML binding and wont be requested from the ServiceProvider.
Usually controls are spawned from the UI rather than from the codebehind...
Nice ..
How do i then fetch my services in various usercontrols ? :)
eg, if the service isn't injected via constructor, then how do you fetch it ?
i'm guessing something a like: ServiceProvider.Current.GetInstance();
but ServiceProvider isn't globally available ?