Mvvm 模型 ViewModel [英] Mvvm model ViewModel

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

问题描述

是否可以命名为MVVM模型?因为 View 通过视图模型数据.View 应该只与 ViewModelData 交互吗?我确实在某处读到正确的 MVVM 模型应该在 ViewModel 中而不是在模型中实现 INotify.是吗?

It can be named MVVM model or not? Because View interacts with DataModel through ViewModelData. Does View should interact only with ViewModelData? I did read somewhere that right MVVM model should implement INotify in ViewModel not in Model. Is it right?

namespace WpfApplication135
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModelData();
    }
}
public class ViewModelData
{
    public DataModel DM { get; set; }
    public ViewModelData()
    {
        DM = new DataModel();
    }
}
public class DataModel : INotifyPropertyChanged
{
    public int label;
    public int Label
    {
        get
        {
            return label;
        }

        set
        {
            label = value;
            RaisePropertyChanged("Label");
        }
    }
    public DataModel()
    {
        Action Update = new Action(Run);
        IAsyncResult result = Update.BeginInvoke(null, null);
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
    public void Run()
    {
        int i=0;
        while(true)
        {
            System.Threading.Thread.Sleep(2000);
            Label = ++i;
        }
    }
}
}

xml

    <Grid>
    <Label Content="{Binding DM.Label}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>

</Grid>

推荐答案

MVVM 最初的想法确实是 View 不应该知道(不依赖)Model.

The initial thought for MVVM was indeed that the View should not know (not depend on) the Model.

在实践中,这意味着在 ViewModel 中重新实现所有这些模型属性(见下图中的浅黄色框),需要大量工作.当您的模型可以轻松实现 INPC 时,例如当它从数据库模式生成时,会更加痛苦.数据库优先模式下的实体框架允许您通过 T4 模板注入 INPC 代码.

In practice this meant re-implementing all those Model properties in the ViewModel (see the light-yellow box in the picture below), a lot of work. And extra painful when your Model can easily implement INPC, for example when it is generated from a database schema. Entity Framework in database-first mode lets you inject the INPC code through the T4 templates.

很快就达成共识,可以转发 ViewModel.Model 属性并绑定到该属性,就像您的 DM 属性一样.请参阅图片中的浅蓝色框.

The consensus quickly became that it is OK to forward a ViewModel.Model property and bind to that, just like your DM property. See the light-blue box in the picture.

问题在这张图片中得到了很好的形象化,注意右上角的大量箭头.它们描述了数据绑定的各种解决方案,您可以使用它们的任意组合.

The issue is visualized well in this picture, note the large number of arrows in the top right corner. They depict the various solutions for databinding and you can use any combination of them.

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

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