gpt4 book ai didi

wpf - 将模型绑定(bind)到 ViewModel (WPF)

转载 作者:行者123 更新时间:2023-12-04 23:19:58 25 4
gpt4 key购买 nike

我正在从 MVP 迁移到 MVVM,对于如何最好地绑定(bind) ViewModel 有点困惑。到Model .我了解我们如何利用 WPF 的数据绑定(bind)基础结构在 View 之间路由事件。和 ViewModel使用 ICommandINotifyPropertyChanged接口(interface),例如 View :

public class MyView
{
public MyView()
{
InitializeComponent();
DataContext = new MyViewModel();
}
}

ViewModel :

public class MyViewModel : INotifyPropertyChanged
{
public MyViewModel(){}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

public ICommand MyCommand ...
}

这很好用!

现在,通常使用 MVP 我会拥有我的 Presenter持有对 Model 的引用通过构造函数注入(inject),并在 Model 上引发事件来自 Presenter更新 Model 中的数据.我用 MVVM 尝试了相同的方法,但这需要 ViewModel采取 Model作为其构造函数中的依赖项,在没有某种形式的 IOC(至少使用 WPF)的情况下直接使用 MVVM 时,这似乎使事情变得有点困惑。

所以,我的两个问题是:
  • 正在注入(inject) Model进入ViewModel正确的方法,或者我应该实现INotifyPropertyChanged Model上的界面并利用 WPF 的绑定(bind)基础设施?
  • 为了获得 MVVM 的好处,您应该几乎总是使用 IOC 和 DI 容器来实现它,还是更好的是 Prism?
  • 最佳答案

  • 这是“纯”MVVM 方法:View必须只依赖于 ViewModel . ViewModel本身就是View之间的桥梁和 Model .
    Model 的动机——定义和职责, ViewViewModel还有他们的关系:

    • The Model, which provides a view-independent representation of your business entities. The design of the model is optimized for the logical relationships and operations between your business entities, regardless of how the data is presented in the user interface.
    • The View class which is the user interface. It displays information to the user and fires events in response to user interactions.
    • The ViewModel class, which is the bridge between the view and the model. Each View class has a corresponding ViewModel class. The ViewModel retrieves data from the Model and manipulates it into the format required by the View. It notifies the View if the underlying data in the model is changed, and it updates the data in the Model in response to UI events from the View.

    -- Implementing the Model-View-ViewModel Pattern, MSDN.



    结论。注入(inject)Model进入ViewModel似乎是正确的方法。此外,而不是注入(inject) Model注入(inject)可能很有用:
  • Model Factory创建 Model — 延迟初始化;
  • Service ( Service Facade ) 检索 Model — 延迟加载。
  • "MVVM Unleashed" book by Michael Brown所示,可以利用以下 MVVM 的潜在优势:可维护性、可测试性、“可混合性”、可移植性。至少,依赖注入(inject)(在所描述的情况下使用依赖注入(inject)容器)允许设计遵循依赖倒置原则:

    The principle of dependency inversion is at the root of many of the benefits claimed for object-oriented technology. Its proper application is necessary for the creation of reusable frameworks. It is also critically important for the construction of code that is resilient to change. And, since the abstractions and details are all isolated from each other, the code is much easier to maintain.

    -- The Dependency Inversion Principle, Robert C. Martin, 1996.



    因此,当遵循依赖倒置原则时,MVVM 的可维护性和可测试性等优点似乎得到了改善。依赖注入(inject)容器只是一个遵循原则的工具。
  • 关于wpf - 将模型绑定(bind)到 ViewModel (WPF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869189/

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