So I have two separate views in a WPF Prism app. How can I inject the same instance of a viewmodel into both the views via Depencency Injection?
+1
A:
(I assume you are using Unity as you mentioned DI directly and are not saying "Import" or similar)
Hi,
You will have to register the ViewModel as a singleton (using a ContainerControlledLifetimeManager) in the container and have it injected as usual. This ensures that you will get the same instance whenever you request for it. A valid approach in this scenario might be to use a named registration in case you want to get the same VM injected elsewhere, but you don't need the same instance.
This article explains how to do this.
Damian Schenkelman
2010-10-18 12:32:03
Many thanks. Actually the ExternallyControlledLifetimeManager was what I really needed. You sent me in the right direction.
NVM
2010-10-18 13:59:39