WPF Prism Controller in a Parent Child Scenario

Topics: Prism v4 - WPF 4
Jul 24, 2012 at 10:47 PM

I have a set of Forms that i need to create under my Parent View which is also my parent object.

In my Parent View i have a ContentControl with a region ChildRegion which loads my ChildView.

But all the child objects in my Child View need to point to the Parent Object.

How can i acheive this is WPF Prism using controllers?

 

Developer
Jul 25, 2012 at 8:53 PM

Hi,

If I am understanding your scenario correctly, you have a parent view (with its own parent view model) which contains a region where a child view (with its own child view model) is injected and what you want is your child view model to access / communicate with your parent view model.

As a starting point, communication between the two view models can be achieved using a shared service, the event aggregator or other communication mechanism provided by Prism. You can find more information about this in the following chapter of the Prism documentation:

If instead you want to access to the parent view model itself a possible approach could be to register the parent view model as a singleton in the container. Like this each time a parent view model is required, the container will always inject the same instance. You can use this approach to inject the same parent view model in both the parent view and the child view.

Please let us know if we have misunderstood your scenario.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Jul 26, 2012 at 5:23 PM

DCherubini,

From reading information on the link you posted, shared services are needed for cross module communucation, but in my case the views are in the same module.

Developer
Jul 26, 2012 at 9:39 PM
Edited Jul 26, 2012 at 9:44 PM

Hi,

As mentioned in the previous answer, Prism provides several communication approaches: shared services, event aggregation, region context and solution commanding, each with their own capabilities and restrictions. Which one you should use would depend mostly of your personal preferences and the requirements of your scenario. You can find more information about the aforementioned approaches in the aforementioned chapter of Prism's documentation.

Regarding shared services, as far as I know there is not an specific restriction about "where" they should be used. As you said, shared services are useful for cross module communications; however, I believe a shared service could also be used to communicate components of the same module. If using a shared service is not suitable in your scenario, you could follow any of the other aforementioned communication approaches.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Jul 30, 2012 at 6:11 PM

DCherubini,

Can you show me an example of how to implement a Shared Service? I am having a hard time finding on the internet.

Developer
Jul 30, 2012 at 8:09 PM

Hi,

For examples on how to implement a Shared Service, I believe you could check the MarketFeedService.cs and MarketHistoryService.cs files in the the Stocktrader RI (in this sample the services are exported into the MEF container) or the OrdersRepository.cs in the Commanding Quickstart where the Unity container is used, both samples are provided along with the Prism library.

As you may find a Shared Service is a class registered in a container that can be accessed through a common interface. Typically, shared services are found in a shared assembly, but this is not always the case, for example in the Commanding Quickstart you can find that the aforementioned service is registered and consumed within the same module.

Additionally, I believe you could also check the Dependency Injection Lab of the Prism Training Kit, which explains step by step how to register and consume services in a decoupled way.

On the other hand, take into account that there are several communication approaches that can be used between components and depending on your scenario you will have to decide which one suits your needs. For example if loose coupling is not a requirement, perhaps using .NET Framework events could be the most simple and straightforward approach.

I hope you find this helpful,

Agustin Adami
http://blogs.southworks.net/aadami