ptsoccer
ptsoccer

Reputation: 158

Returning Data to Previous a ViewModel using Prism Navigation

Imagine you have two views, ViewModel InfoViewModel which displays information about a person and ViewModel SearchViewModel which handles searching for a person and selecting a result.

I want InfoViewModel to be able to initiate a search process which will eventually result in SearchViewModel telling view InfoViewModel that someone has been selected. Another potential requirement would be that InfoViewModel can communicate with SearchViewModel to say that this result is unacceptable and that the user should pick someone else (this should happen without the search result screen disappearing and reappearing)

Normally this would be easy to solve by InfoViewModel giving SearchViewModel a callback that would be called whenever a result is selected. The callback could also having a return parameter specifying whether the result is good or not so that SearchViewModel can clean itself up or not. However it almost seems like Prism discourages this since, at least in prism 4.0, there was no good way to navigate with an object as a parameter (you could only pass strings as parameters). One of the workarounds I'm using is to use a service to store object parameters which can then be uniquely identified in the next view by way of a string guid. This has it's own problems, but worse it feels like Prism intentionally designed against doing this.

Another way seems to be using the IEventAggregator to broadcast an event when a person is selected. I would have to filter each event based on a unique id to make sure the event I received is for me (in case for some reason there are two search processes going on). It feels like this way is just trying to emulate a direct callback but in a roundabout way.

Does anyone know of a good solution to this? If the answer is there is no good way in Prism 4.0 to solve this, you have to be on Prism 5.0 (since it supports passing object parameters) then that's fine too. I'm just wondering what other ideas are out there

Upvotes: 0

Views: 611

Answers (1)

Jason
Jason

Reputation: 647

There probably isn't just one "right" answer, there are just different approaches.

You can navigate to the view passing details using the NavigationParameters class in Prism5. If you are limited to Prism4, you could place the parameters in the RegionContext and then access them in OnNavigatedTo method of the the INavigationAware interface.

With regards to "returning" results, that might depend on the layout of the application. If multiple views are allowed, registering and then using an event through the event broker is the recommended way to communicate between different components. If you have just one view and you feel the event broker is unnecessary and perhaps overkill, consider using a service paired with the SearchViewModel to store the results (not dissimilar to what you appear to be doing already). Any other ViewModel can be injected with the service, so they will be able to access any property details that the SearchViewModel has set.

If the searching is limited logically within the parent view, consider using an InteractionRequest to popup the search view.

Upvotes: 1

Related Questions