谁应该在MvvmCross中创建视图模型实例 [英] Who should create view model instances in MvvmCross

查看:61
本文介绍了谁应该在MvvmCross中创建视图模型实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅需说明一下:我知道MvvmCross对于在何处以及如何创建视图模型非常灵活.我的问题更多是关于适当分离关注点,以简化复杂的跨平台应用程序的设计.

Just to make it clear: I know MvvmCross is very flexible about where and how view models can be created. My question is more about proper separation of concerns, to simplify design of complex cross-platform applications.

考虑一下,我们有一个带有客户列表和客户详细信息的应用程序.在iPad和Surface上,列表和详细信息显示在同一页面上,但是在较小的设备上,选定列表的客户列表和详细信息会在不同的页面中分开显示.因此,我们有一个带有CustomerListViewModel和CustomerDetailsViewModel的PCL.现在我们应该如何在可移植类库中管理视图模型的生命周期?

Consider we have an app with customer list and customer details. On iPad and Surface the list and details are shown on the same page, but on smaller devices customer list and details for a selected customer are split between separate pages. So we have a PCL with CustomerListViewModel and CustomerDetailsViewModel. Now how should we manage view model lifetime from within the portable class library?

我最初使用的是CustomerListViewModel实现中的代码,如下所示:

I originally did it using code in CustomerListViewModel implementation that looks like this:

public ICommand SelectCustomerCommand
{
    get { return new MvxCommand(SelectCustomer); }
}

public void SelectCustomer()
{
    if (formFactor == FormFactor.Phone)
    {
        ShowViewModel<CustomerDetailsViewModel>(new CustomerDetailsViewModel.NavObject(this.SelectedCustomer));
    }
    else
    {
        this.CustomerDetails = new CustomerDetailsViewModel(this.SelectedCustomer);
    }
}

在这里重要的是,我们要么调用ShowViewModel,然后依次要求演示者构造一个CustomerDetailsViewModel对象并将其呈现在新页面中,或者显式创建一个CustomerDetailsViewModel实例并将其绑定到CustomerDetails.

What is essential here is that we either call ShowViewModel that in turns asks a presenter to construct a CustomerDetailsViewModel object and render it in a new page or explicitly create an instance of CustomerDetailsViewModel and bind it to CustomerDetails.

看过N + 1个MvvmCross视频系列的第32集和第42集之后,我不确定这是否是正确的方法.当然可以,但是视图模型应该关心其他视图模型的实例化细节吗?

After having seen episodes 32 and 42 of N+1 MvvmCross video series I am not quire sure this is the right way to do it. Of course it works, but should a view model care about instantiation details of other view model?

我的第二个想法是重构此代码并将此逻辑放入演示者,因此我可以简单地编写CustomerListViewModel实现:

My second thought was to refactor this code and put this logic into a presenter, so I can simply write in CustomerListViewModel implementation:

public void SelectCustomer()
{
    ShowViewModel<CustomerDetailsViewModel>(new CustomerDetailsViewModel.NavObject(this.SelectedCustomer));
}

...,演示者将在ShowViewModel调用触发的代码内完成其余工作.但是,在第42集中,展示了如何直接从关联的视图控制视图模型的生命周期:

... and presenter will do the rest inside the code triggered by ShowViewModel call. However, in the episode 42 it's shown how to control view model lifetime right from the associated view:

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);
    VisibleViewModel.IsVisible(false);
    if (e.NavigationMode == NavigationMode.Back)
        KillableViewModel.KillMe();
}

但是,如果视图模型的生命周期由演示者控制,我们是否不应该尝试将KillMe调用置于演示者的逻辑之内?我知道这么少的代码并没有多大区别,但是将它放在presenter的类中并减少代码的隐藏不是一个优势吗?

But if a view model lifetime is controlled by a presenter, shouldn't we try to place KillMe call inside presenter's logic? I know so small piece of code doesn't make much difference but couldn't this be an advantage to put it in presenter's class and reduce code-behind?

推荐答案

当然,ViewModel不应处理任何有关视图(屏幕)的事情.

Definitely the ViewModel should not handle anything in regards to view (screen).

我有一个简单的想法是使用自定义演示者,该演示者能够基于ShowViewModel<>请求创建视图.

One quick idea I have is use a custom presenter which is able to create views based on the ShowViewModel<> requests.

自定义演示者是视图责任,因此您可以测试屏幕方向.

The custom presenter is a view responsibility so you can test for screen orientation.

http://slodge.blogspot.co.uk/2013/06/presenter-roundup.html

这篇关于谁应该在MvvmCross中创建视图模型实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆