I have a ViewModel that has a command that opens another view:
public ICommand OpenAnotherViewCommand
{
get
{
return new MvxCommand(() => ShowViewModel<AnotherViewModel>());
}
}
So far, so good. Then in AnotherViewModel
I want to be able to go back to the first view model. Originally I did something like this:
public ICommand ReturnCommand
{
get
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
// Add some stuff from this model to pass to the first view model
return new MvxCommand(() => {
ShowViewModel<FirstViewModel>(parameters);
}
}
}
I added an InitFromBundle
to my first view model and this worked too. However, then I realized that my original first view model still existed (I noticed this because of some event handlers that seemed to be firing multiple times!). My ShowViewModel
created an new FirstViewModel
, but the old one was never destroyed (it seems really obvious now). So the stack of views is now first -> another -> first
when it ought to be just first
.
So after face palming over that I replaced my ShowViewModel
in ReturnCommand
with Close(this)
and now I've fixed the navigation problem and I'm not producing a long line of unneeded view models. However, what I've lost is the ability to pass back data from AnotherViewModel
to the first one.
So how can I pass data back to my first view model when the second one is closed?