0

I'm working on figuring out mvvmcross and if I can use it for an iPhone (and eventually iPad and Android) app I'm going to develop. The MVVM pattern is really powerful and works great for me, but I have several views where I need to add a navigation control that will allow the user to jump to several different other views and I'm wondering what's the best way to do it.

Right now, I've created a NavigationControlViewModel which exposes a collection of NavigationLinkViewModel which have a link text property and a command that will show the appropriate view. But to add this to a view for, say, MyViewModel is a little tricky. Right now what I've done is add the NavigationControlViewModel to MyViewModel so that I can bind it in MyView:

    private NavigationControlViewModel _nav;
    public NavigationControlViewModel Navigation {
        get {
            _nav = _nav ?? new NavigationControlViewModel (Mvx.Resolve<INavigationService> ());  
            return _nav;
        }
    }

This works, but doesn't seem as nicely contained as I'd like to be. I still need to add controls to MyView for the NavigationControlViewModel and then add it to every other view that needs it (as well as adding it to their view models).

What would be the best practice for handling this sort of thing in iOS and MVVM?

I've seen the video on using a split view, but I'm not sure if that's the best approach. I need a vertical split and I only need it on some views, not every view.

4

1 回答 1

1

为了在视图模型之间共享导航机制,我想您可以像以前那样使用聚合,也NavigationControlViewModel可以对类中的所有导航项使用继承BaseViewModel

我个人很乐意使用其中任何一个,但会确保将我的所有导航选项公开为ICommands - 仅仅是因为这是 .Net 样式数据绑定通常期望呈现“动作挂钩”的方式。请注意,有一种生成 ICommand 的反射方式 - 请参阅http://slodge.blogspot.co.uk/2013/03/fixing-mvvm-commands-making-hot-tuna.html

对于在屏幕上实际展示ViewModelvia a View... 我鼓励你相信你可以做任何你和你的 UX/设计团队想做的事情。

一些标准的展示方法可以通过:UINavgiationController, UISplitViewController, UITabBarViewController,UIPopupViewPresentModalViewController- 你可以自由地使用它们并将它们组合在一起 - 例如你可以有一个导航控制器,它有两层深显示一个模态视图,它包含一个带有两个的拆分视图孩子们...

除了标准方法之外,人们还选择了许多其他 UI 设计范例:

View默认情况下,MvvmCross 为您提供了一个“整页”演示器,它ViewModelUINavigationController. 正如您在提到的 N+1 视频中看到的那样,您可以轻松地覆盖该行为,然后您可以选择呈现View-ViewModel以您喜欢的任何方式配对 - 例如,您可以选择呈现整个页面,一些使用飞出,然后是一些使用制表符。

因为IMvxTouchView演示者只是 C# 代码,而且我们的开发人员喜欢编写 C# 代码,所以我们可以在演示者中实现我们想要的任何精彩逻辑,包括测试当前显示的内容以确定在何处显示下一页的代码。

我无法评论使设计看起来不错的“最佳实践”。

但我确实相信,如果您坚持通过ShowViewModelthen 展示您的视图模型,那么您可以在每个平台上使用哪种展示策略时获得最大的灵活性。

有关演示者的更多信息,请访问http://slodge.blogspot.co.uk/2013/06/presenter-roundup.html

于 2013-06-11T14:00:58.417 回答