I am using prism for Xamarin.Forms - it's been great so far (in terms of its coupling with Unity and navigation, etc). However, I am confused by one thing... How am I suppose to handle instance dependencies within VMs when navigating between parent and child. For example:
Say I have a VM that supports a View which lists out nested some objects. The VM populates these objects in its OnNavigatedTo method using an object from the nav params that it will use to query the backend with.
public override async void OnNavigatedTo(NavigationParameters parameters)
{
if (parameters.ContainsKey(Constants.Navigation.MasterObject))
{
CurrentMaster = parameters.GetValue<MasterObjectFacade>(Constants.Navigation.MasterObject);
await RefreshAsync(); //this calls the api using the CurrentMaster property
}
else
{
CurrentMaster = new MasterObjectFacade(new MasterObject{ Id = 1000 });
Title = CurrentMaster.Id.ToString();
}
}
Then when one of the child (nested) objects from the master object is selected a DelegateCommand fires off something like this:
new DelegateCommand(async () => await NavigationService.NavigateAsync("ChildObject1",
new NavigationParameters { { Helpers.Constants.Navigation.ChildObject1, CurrentMaster.ChildObject1} }));
Which works fine until the user hits back on the navigation within the child.... causing the "OnNavigatedTo" in the parent VM to be called (makes sense) but the parameter for MasterObject is null - which also makes sense, the instance of MasterObject is lost.
So my question is, should I pass the instance of MasterObject to the child and then back from the child when navigating between parent and child or is there some different way to do this that I am not aware of (like using messaging)? Or am I running into this issue because I am violating some mvvm rules...
thanks.