MVVMLight - ViewModel referencing other ViewModels
HI,
I'm using MVVMLight and i would like a particular VM to reference to another VM's property. Currently, i using the ViewModelLocator.SomeViewModelStatic to do it. At times, i would also like to use the metho开发者_StackOverflow社区ds of another viewmodel. And also using the ViewModelLocator.SomeViewModelStatic to use other ViewModels' methods.
Is this the correct way of communicating between ViewModels?
In such case I would start by adding Inversion of Control container to the application (Autofac, Ninject or even MEF (if you're familiar with it)) and registering within all ViewModels (you can do it manually or same IoC containers can look for specific classes in the assemblies) and Services. Then I would implement ViewModelLocator
to work with the IoC in such a way that ViewModels would be created by it (then every dependency would be resolved by Dependency Injection and all you have to worry about is to register everything in IoC)
By using IoC you can also use factory methods, that can create instances for you in ViewModels.
At times I build a tree of VM's. E.g.: I create a PersonHobbies VM by adding both a Person and a Hobbies VM to the PersonHobbies VM. This class will have all the commands that span both other VM's.
I hesitate to link the Hobbies VM to the Person VM (or reverse) because it will become harder to use the Hobbies VM without the Person VM (or reverse).
Similarly I create Views: In this example I could create Views for both Person and Hobbies and use these Views when constructing the PersonHobbies View.
精彩评论