视图模型MVVM设计问题 [英] A view model mvvm design issue

查看:129
本文介绍了视图模型MVVM设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解释的最佳方式是例如这样:

the best way to explain is with example so:

这是模型

public class Person 
{
    public int age;
    public string name;
}

这是视图模型

public class PersonVM
{    
}

我的问题是:结果
应该在VM揭露人的数据模板或与他自己的属性封装模特属性

my question is:
should the vm expose the person to the data template or encapsulate the model properties with his own properties?

推荐答案

视图模型应该定义它自己的属性和隐藏从视图模型的细节。这给了你最大的灵活性,并有助于保持视图模型型问题漏入模型类。通常您的视图模型类封装了委托模型。例如,

The view model should declare its own properties and hide the specifics of the model from the view. This gives you the most flexibility, and helps keep view model-type issues from leaking into the model classes. Usually your view model classes encapsulate the model by delegation. For example,

class PersonModel {
    public string Name { get; set; }
}

class PersonViewModel {
    private PersonModel Person { get; set;}
    public string Name { get { return this.Person.Name; } }
    public bool IsSelected { get; set; } // example of state exposed by view model

    public PersonViewModel(PersonModel person) {
        this.Person = person;
    }
}



记住:模型不应该知道的事情正消耗它视图模型和视图模型不应该知道关于消耗的视图什么。视图应一无所知潜伏在后台模型。因此,封装在视图模型属性背后的模式。

Remember: the model shouldn't know anything about the view model that is consuming it, and the view model shouldn't know anything about the view that is consuming it. The view should know nothing about the models lurking in the background. Thus, encapsulate the model behind properties in the view model.

这篇关于视图模型MVVM设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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