Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass an object when navigating to a new view in PRISM 4

I am working on a PRISM application where we drill down into the data (to get more details). In my implementation I have a nested MVVM and when I navigate down the tree I would like to pass a model to a my newly created view.

As far as I know, currently PRISM allows to pass strings, but doesn't allow to pass objects. I would like to know what are the ways of overcoming this issue.

like image 491
Vitalij Avatar asked Apr 11 '11 17:04

Vitalij


3 Answers

i usually use a service where i register the objects i want to be passed with a guid. these get stored in a hashtable and when navigating in prism i pass the guid as a parameter which can then be used to retrieve the object.

hope this makes sense to you!

like image 197
Markus Hütter Avatar answered Sep 23 '22 18:09

Markus Hütter


I would use the OnNavigatedTo and OnNavigatedFrom methods to pass on the objects using the NavigationContext.

First derive the viewmodel from INavigationAware interface -

 public class MyViewModel : INavigationAware
 { ...

You can then implement OnNavigatedFrom and set the object you want to pass as navigation context as follows -

void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
     SharedData data = new SharedData();
     ...
     navigationContext.NavigationService.Region.Context = data;
}

and when you want to receive the data, add the following piece of code in the second view model -

void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
    if (navigationContext.NavigationService.Region.Context != null)
    {
                if (navigationContext.NavigationService.Region.Context is SharedData)
                {
                    SharedData data = (SharedData)navigationContext.NavigationService.Region.Context;
                    ...
                }
    }
}

ps. mark this as answer if this helps.

like image 37
whihathac Avatar answered Sep 24 '22 18:09

whihathac


PRISM supports supplying parameters:

var para = new NavigationParameters { { "SearchResult", result } };
_regionManager.RequestNavigate(ShellRegions.DockedRight, typeof(UI.SearchResultView).FullName, OnNavigationCompleted, para);

and implement the INavigationAware interface on your View, ViewModel or both.

you can also find details here: https://msdn.microsoft.com/en-us/library/gg430861%28v=pandp.40%29.aspx

like image 20
uTILLIty Avatar answered Sep 23 '22 18:09

uTILLIty