将pop / back事件中的数据传递给先前的视图模型

时间:2014-08-21 08:55:57

标签: xamarin.ios xamarin mvvmcross

我有这样的结构:

SessionsView - > CreateSessionView

使用像这样的视图模型:

SessionsViewModel - Session个对象的列表

CreateSessionViewModel - 单个Session对象

用户在创建会话中填写表单,填充视图模型上的Session对象,完成命中,然后调用:NavigationController.PopViewControllerAnimated(true)将其带回会话列表。

有没有办法将我新创建的会话对象传递回以前的视图Viewmodel并将其添加到会话对象列表中?我知道如何在ShowViewModel<TYPE>(PARAM)命令中传递params,但不确定如何在导航时执行此操作。

更新1:

我找到了'一种'做法的方法..虽然感觉不太好:

var sessionsView = (SessionsView)NavigationController.ViewControllers.FirstOrDefault(vc => vc is SessionsView);
                    var sessionsViewModel = (SessionsViewModel)sessionsView.ViewModel;
                    sessionsViewModel.Sessions.Add(vModel.Session);
                    NavigationController.PopViewControllerAnimated(true);

1 个答案:

答案 0 :(得分:1)

只需使用PopViewControllerAnimated的返回参数(bool动画)。

NavigationController.PopViewControllerAnimated(true);
ViewControllerClass viewController = (ViewControllerClass)NavigationController.TopViewController;
viewController.StoreSessionObject(session); <-- you need to create this method