Mvvm模型ViewModel [英] Mvvm model ViewModel

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

问题描述

它可以命名为MVVM模型吗?因为View通过以下方式与DataModel进行交互 ViewModelData. View是否应仅与ViewModelData交互?我确实读过某个地方,正确的MVVM模型应该在ViewModel中而不是在Model中实现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;
        }
    }
}
}

xaml

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

</Grid>

推荐答案

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

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

在实践中,这意味着在ViewModel中重新实现所有那些Model属性(请参见下图的浅黄色框),需要进行大量工作.当您的模型可以轻松实现INPC时(例如,从数据库模式中生成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天全站免登陆