ViewModelLocator Resolves Multiple Times (SL4)

Apr 14, 2011 at 7:36 PM

Hi,

If you put a Debug.WriteLine(...) in the constructor of a ViewModel (e.g. MyViewModel) which is resolved via the ViewModelLocator it gets resolved multiple times in Silverlight 4 and only once in WPF 4. I would have expected it to be resolves only once ever...

Can someone please tell me why?  I don't believe it is an issue caused by the ViewModelLocator but rather due to some behavior of Silverlight.

This is a pain for me because do all my creation of commands in the constructor.  Any suggestion on how to resolve this would be much appreciated!

Clint
PS This applies to samples on this project too, not st my code. 

Coordinator
Apr 14, 2011 at 10:17 PM

@AnotherWay:

John Papa (the Sr. Tech Evangelist for SL at MS) has some thoughts on doing that with a value converter instead.

http://johnpapa.net/silverlight/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/

More info here:

http://houseofbilz.com/archives/2010/06/04/adventures-in-mvvm-viewmodel-location-and-creation/

For me, it doesn't really affect me because it doesn't really hurt me to construct 6.  They get garbage collected. It is, indeed, irritating, though.

B

Apr 15, 2011 at 6:12 AM

Hi Brian,

Ok, great you are aware of this it's not just me going insane)!

Thanks for the links - will follow those up.

Clint