【发布时间】:2013-08-13 18:36:15
【问题描述】:
我有一个 ViewModel,它有一个打开另一个视图的命令:
public ICommand OpenAnotherViewCommand
{
get
{
return new MvxCommand(() => ShowViewModel<AnotherViewModel>());
}
}
到目前为止,一切都很好。然后在AnotherViewModel 我希望能够回到第一个视图模型。最初我做了这样的事情:
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);
}
}
}
我在我的第一个视图模型中添加了一个InitFromBundle,这也很有效。然而,后来我意识到我最初的第一个视图模型仍然存在(我注意到这一点是因为一些似乎被多次触发的事件处理程序!)。我的ShowViewModel 创建了一个新的FirstViewModel,但旧的从未被销毁(现在看起来很明显)。所以视图堆栈现在是first -> another -> first,而它应该只是first。
因此,在我将ReturnCommand 中的ShowViewModel 替换为Close(this) 之后,我已经解决了导航问题,并且我不会生成一长串不需要的视图模型。但是,我失去了将数据从AnotherViewModel 传回第一个的能力。
那么,当第二个视图模型关闭时,如何将数据传回我的第一个视图模型?
【问题讨论】: