MVVM模型实例化 [英] MVVM model instantiation

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

问题描述

在WPF MvvmFoundation 之后,将视图链接到ViewModel有许多选择,例如

Following WPF MvvmFoundation, linking the View with the ViewModel has many choices like described on http://www.paulstovell.com/mvvm-instantiation-approaches.

但是,他们的示例与如何将ViewModel与 Model 链接无关.

However their example has nothing about how to link the ViewModel with the Model.

传统上,我先创建模型,然后创建一个或多个渲染模型的视图.似乎MVVM促使人们创建视图,该视图创建了ViewModel,后者又创建了Model.我希望不是这样,因为将复杂的业务模型与各种ModelView连接起来可能会很困难.

Traditionally I created the model first and then one or more views that render it. It seems that MVVM pushes people to create the View, which creates the ViewModel, which create the Model. I hope it's not the case as wiring a complex business model with various ModelView can else be tough.

如何在MVVM中实例化业务模型类,并链接它们与ViewModels?

How do you instantiate your business model classes in MVVM and link them with your ViewModels?

推荐答案

我通常将Model对象作为构造函数参数传递给VM.我使用App类作为控制器,它将使用主模型初始化MainWindow,MainWindowViewModel.之后,MainWindowViewModel负责使用适当的模型对象初始化其他VM.

I normally pass Model objects as constructor params to VM. I use App class as the controller which will initialize MainWindow, MainWindowViewModel with the main model. There after the MainWindowViewModel takes care of initializing other VMs with appropriate model objects.

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        mainWindow = new MainWindow();
        mainWindow.DataContext = new MainWindowViewModel(new Model());
        mainWindow.Show();
    }

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

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