WPF的MVVM的简单定义 [英] Nice and simple definition of WPF's MVVM

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

问题描述

WPF的MVVM设计模式中的Model,View和ViewModel对象的基本定义是什么?

解决方案

型号:


  1. 表示数据

  2. 实体。 >
  3. 模型类是封装应用程序数据和业务逻辑的非可视化类。

  4. 他们负责管理应用程序的数据,并通过封装所需的业务规则和数据验证逻辑来​​确保其一致性和有效性。

  5. 模型类不要直接引用视图或查看模型类,并且不依赖于如何实现

  6. 模型类通常通过 INotifyPropertyChanged INotifyCollectionChanged 接口提供属性和收集更改通知事件。

  7. 这允许他们在视图中容易地绑定数据。

  8. 表示对象集合的模型类通常来自 ObservableCollection< T> 类。

  9. 模型类通常通过 IDataErrorInfo INotifyDataErrorInfo 接口。

  10. 模型类通常与封装数据访问和缓存的服务或存储库结合使用。

  11. 不需要知道从哪个WCF服务获取其数据。 WCF RIA服务等。

  12. 可能包含验证。

查看: / strong>


  1. 视图是一个视觉元素,如窗口,页面,用户控制或数据模板。

  2. 视图定义了视图中包含的控件及其外观,视觉布局和样式。

  3. 视图引用视图模型通过其DataContext属性。

  4. 视图中的控件是数据绑定到 ViewModel 公开的属性和命令。

  5. 视图可以自定义视图和视图模型之间的数据绑定行为。
    例如,视图可以使用值转换器来格式化要在UI中显示的数据,或者可以使用验证规则向用户提供额外的输入数据验证。

  6. 视图定义和处理UI视觉行为,例如可能由视图模型中的状态更改触发的动画或过渡,或通过用户与UI的交互。

  7. 视图的代码-behind可以定义UI逻辑来实现在XAML中难以表达的视觉行为,或者需要直接引用视图中定义的特定UI控件。

ViewModel:


  1. ViewModel是一个非可视化类,不派生自任何WPF或Silverlight基类。

  2. 它封装了在应用程序中支持用例或用户任务所需的演示逻辑

  3. ViewModel可以独立于视图和模型进行测试。

  4. ViewModel通常不直接引用视图 。它将具有绑定到视图的UI友好实体,UI状态,操作和公共属性。

  5. 实现视图可以数据绑定的属性和命令

  6. 它通过 INotifyPropertyChanged 和<$ c通过更改通知事件通知任何状态更改的视图$ c> INotifyCollectionChanged 接口。

  7. 与各种命令与视图进行交互。

  8. 视图模型视图与模型的交互

  9. 它可以转换或操作数据,以便它可以被视图轻松消耗,并可能会实现模型上可能不存在的其他属性。

  10. 它还可以通过 IDataErrorInfo INotifyDataErrorInfo 接口来实现数据验证。

  11. 视图模型可以定义视图可视化地呈现给用户的逻辑状态。

  12. 调用服务以在MVVM三元组之外进行通信。 li>

资料来源: http://code.msdn.microsoft.com/Design-Patterns-MVVM-Model-d4b512f0


What is the basic definition of the Model, View and ViewModel objects in WPF's MVVM design pattern? What are their responsibilities, what each of them should and shouldn't do?

解决方案

Model:

  1. Represents the Data.
  2. The Entity.
  3. Model classes are non-visual classes that encapsulate the application's data and business logic.
  4. They are responsible for managing the application's data and for ensuring its consistency and validity by encapsulating the required business rules and data validation logic.
  5. The model classes do not directly reference the view or view model classes and have no dependency on how they are implemented.
  6. The model classes typically provide property and collection change notification events through the INotifyPropertyChanged and INotifyCollectionChanged interfaces.
  7. This allows them to be easily data bound in the view.
  8. Model classes that represent collections of objects typically derive from the ObservableCollection<T> class.
  9. The model classes typically provide data validation and error reporting through either the IDataErrorInfo or INotifyDataErrorInfo interfaces.
  10. The model classes are typically used in conjunction with a service or repository that encapsulates data access and caching.
  11. Not required to know where it gets its data from i.e from a WCF service. WCF RIA Services, etc.
  12. May contain validation.

View:

  1. The view is a visual element, such as a window, page, user control, or data template.
  2. The view defines the controls contained in the view and their look and feel, visual layout and styling.
  3. The view references the view model through its DataContext property.
  4. The controls in the view are data bound to the properties and commands exposed by the ViewModel.
  5. The view may customize the data binding behavior between the view and the view model. For e.g, the view may use value converters to format the data to be displayed in the UI, or it may use validation rules to provide additional input data validation to the user.
  6. The view defines and handles UI visual behavior, such as animations or transitions that may be triggered from a state change in the view model or via the user's interaction with the UI.
  7. The view's code-behind may define UI logic to implement visual behavior that is difficult to express in XAML or that requires direct references to the specific UI controls defined in the view.

ViewModel:

  1. The ViewModel is a non-visual class and does not derive from any WPF or Silverlight base class.
  2. It encapsulates the presentation logic required to support a use case or user task in the application.
  3. The ViewModel is testable independently of the view and the model.
  4. The ViewModel typically does not directly reference the view. It will have UI Friendly Entities, UI State, Actions and Public properties that are bound to a View.
  5. It implements properties and commands to which the view can data bind.
  6. It notifies the view of any state changes via change notification events via the INotifyPropertyChanged and INotifyCollectionChanged interfaces.
  7. Interacts with View with various Commands.
  8. The view model coordinates the view's interaction with the model.
  9. It may convert or manipulate data so that it can be easily consumed by the view and may implement additional properties that may not be present on the model.
  10. It may also implement data validation via the IDataErrorInfo or INotifyDataErrorInfo interfaces.
  11. The view model may define logical states that the view can represent visually to the user.
  12. Invokes services to communicate outside the MVVM triad.

Source: http://code.msdn.microsoft.com/Design-Patterns-MVVM-Model-d4b512f0

这篇关于WPF的MVVM的简单定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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