I’m trying to wrap my head around the dependency injection concept in WPF. If I have a Dialog that has a ViewModel defined as being dependency injected, at what point during runtime is the dependency injected ViewModel initialized and given a value? Is it during the Initialize() method that is called in the Constructor of the Dialog?
public partial class LoginDialog
{
private LoginViewModel _loginViewModel;
[Dependency]
public LoginViewModel LoginViewModel
{
get { return _loginViewModel; }
set
{
...
}
}
public LoginDialog()
{
InitializeComponent();
}
}
The answer to your question is: It depends (no pun intended). It depends on a lot of things, which DI framework are you using (Unity, Ninject, etc)? In Unity (I have more experience with this one) you have to define what the lifetime scope of an object is, and depending on it’s lifetime, you may get things done at different times.
To try to be more specific: Generally, a dependency property as you demonstrated there, will be “set” when the object on which it is defined is instantiated. So when your
LoginDialogis instantiated, theLoginViewModelshould be set.