Prism+MEF+MVCVM: injecting viewmodel instance of the currently active view into Contoller

Topics: Prism v4 - WPF 4
Feb 8, 2014 at 2:54 PM
Hi,

This is an analogy for my case (where i use Model-View-Controller-ViewModel approach where most VM logic implemented in controller)
: i have CustomerViewModel which is the VM for CustomerView. now when i select certain customerID from a list (in another view)
so the details of that customer are displayed (CustomerName, Address, ....) in a new tap in the main region. i use MEF to inject CustomerViewModel into the CustomerController
>in the controller how i get reference to the CustomerViewModel instance of the active CustomerView as i need to get data from that instance to use it in another scenario

>if a command (AddCustomerCommand) in the VM is fired how to handle that command in the controller so that the Commands in the VM are only placeholders for the view and controller listen to the event of command firing and handle it and return the result to the VM via the injected instance.
Thank you very much for bearing with us.
Feb 10, 2014 at 4:00 PM
Hi jivara,

If I understood correctly, you would want to get the reference of the active CustomerViewModel instance when a command on the CustomerView is fired and then, handle the command on the CustomerController.

One possible and decoupled-simple way to do this is by using EventAggregation service. To do this, you would need to Subscribe the CustomerController to an event, which it then will be published by the CustomerViewModel active instance. You could raise the event in the CustomerViewModel's Command handler method so the Controller is notified that the corresponding command to be handled was fired. Therefore, you would finally handle the command on the CustomerController's event handler.

In addition, you would be able to pass the ViewModel reference as a parameter on the published event, which should be defined with a specified parameter type.

You may find more related information on the following MSDN Prism Guide section:

I hope this helped you,

Regards.
Gabriel Ostrowsky
https://blogs.southworks.net/gostrowsky
Feb 12, 2014 at 3:07 PM
Hi Gabriel,
sorry for being late due hard circumstances. your answer is ok just one thing: how to get reference to the VM of the current active view in the controller but without command triggering from within VM? Sometimes i need this reference to get data that i use in calculations that concern another VM.

Thanks
Feb 14, 2014 at 5:17 PM
Hi jivara,

I am afraid I don't completely understand the flow of your App. Based on my understanding, there would be some scenarios where the active View's ViewModel reference would be needed, but without having triggered any command on its corresponding View. Then, the action may come from other concerning ViewModel.

Does the Controller present a reference to the active View? If that is the case, you could simply call View.DataContext() from the Controller and retreive the active View's ViewModel. Otherwise, I would like to know how is your app structured in order to have a better understanding on communication.

If there is anything I am not considering, I would appreciate if you could describe me a detailed specific scenario you would need to solve.

Regards.
Gabriel Ostrowsky.
https://blogs.southworks.net/gostrowsky
Feb 22, 2014 at 3:20 PM
Hi Gabriel,
Sorry for being late for reply.Your Answer is satisfactory, Thanks for your valuable help.