gpt4 book ai didi

wpf - MVVM 模型实例化

转载 作者:行者123 更新时间:2023-12-04 01:47:49 26 4
gpt4 key购买 nike

关注 WPF MvvmFoundation ,将 View 与 ViewModel 链接有很多选择,如 http://www.paulstovell.com/mvvm-instantiation-approaches 中所述.

然而,他们的例子没有关于如何将 ViewModel 与 联系起来。型号 .

传统上,我首先创建模型,然后创建一个或多个渲染它的 View 。好像MVVM是在插入人们创建View,ViewModel创建ViewModel,ViewModel创建Model。我希望情况并非如此,因为将复杂的业务模型与各种 ModelView 连接起来可能会很困难。

你如何实例化你的商业模式类在 MVVM 和 中链接 他们与您的 ViewModels?

最佳答案

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

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

关于wpf - MVVM 模型实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6126716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com