本文介绍了WPF的MVVM的简单定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
WPF的MVVM设计模式中的Model,View和ViewModel对象的基本定义是什么?解决方案
型号:
- 表示数据。
- 实体。 >
- 模型类是封装应用程序数据和业务逻辑的非可视化类。
- 他们负责管理应用程序的数据,并通过封装所需的业务规则和数据验证逻辑来确保其一致性和有效性。
- 模型类不要直接引用视图或查看模型类,并且不依赖于如何实现。
- 模型类通常通过
INotifyPropertyChanged
和INotifyCollectionChanged
接口提供属性和收集更改通知事件。 - 这允许他们在视图中容易地绑定数据。
- 表示对象集合的模型类通常来自
ObservableCollection< T>
类。 - 模型类通常通过
IDataErrorInfo $ c $提供数据验证和错误报告c>或
INotifyDataErrorInfo
接口。 - 模型类通常与封装数据访问和缓存的服务或存储库结合使用。
- 不需要知道从哪个WCF服务获取其数据。 WCF RIA服务等。
- 可能包含验证。
查看: / strong>
- 视图是一个视觉元素,如窗口,页面,用户控制或数据模板。
- 视图定义了视图中包含的控件及其外观,视觉布局和样式。
- 视图引用视图模型通过其DataContext属性。
- 视图中的控件是数据绑定到由 ViewModel 公开的属性和命令。
- 视图可以自定义视图和视图模型之间的数据绑定行为。
例如,视图可以使用值转换器来格式化要在UI中显示的数据,或者可以使用验证规则向用户提供额外的输入数据验证。 - 视图定义和处理UI视觉行为,例如可能由视图模型中的状态更改触发的动画或过渡,或通过用户与UI的交互。
- 视图的代码-behind可以定义UI逻辑来实现在XAML中难以表达的视觉行为,或者需要直接引用视图中定义的特定UI控件。
ViewModel:
- ViewModel是一个非可视化类,不派生自任何WPF或Silverlight基类。
- 它封装了在应用程序中支持用例或用户任务所需的演示逻辑。
- ViewModel可以独立于视图和模型进行测试。
- ViewModel通常不直接引用视图 。它将具有绑定到视图的UI友好实体,UI状态,操作和公共属性。
- 它实现视图可以数据绑定的属性和命令。
- 它通过
INotifyPropertyChanged
和<$ c通过更改通知事件通知任何状态更改的视图$ c> INotifyCollectionChanged 接口。 - 与各种命令与视图进行交互。
- 视图模型视图与模型的交互。
- 它可以转换或操作数据,以便它可以被视图轻松消耗,并可能会实现模型上可能不存在的其他属性。
- 它还可以通过
IDataErrorInfo
或INotifyDataErrorInfo
接口来实现数据验证。 - 视图模型可以定义视图可视化地呈现给用户的逻辑状态。
- 调用服务以在MVVM三元组之外进行通信。 li>
资料来源:
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:
- Represents the Data.
- The Entity.
- Model classes are non-visual classes that encapsulate the application's data and business logic.
- 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.
- The model classes do not directly reference the view or view model classes and have no dependency on how they are implemented.
- The model classes typically provide property and collection change notification events through the
INotifyPropertyChanged
andINotifyCollectionChanged
interfaces. - This allows them to be easily data bound in the view.
- Model classes that represent collections of objects typically derive from the
ObservableCollection<T>
class. - The model classes typically provide data validation and error reporting through either the
IDataErrorInfo
orINotifyDataErrorInfo
interfaces. - The model classes are typically used in conjunction with a service or repository that encapsulates data access and caching.
- Not required to know where it gets its data from i.e from a WCF service. WCF RIA Services, etc.
- May contain validation.
View:
- The view is a visual element, such as a window, page, user control, or data template.
- The view defines the controls contained in the view and their look and feel, visual layout and styling.
- The view references the view model through its DataContext property.
- The controls in the view are data bound to the properties and commands exposed by the ViewModel.
- 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.
- 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.
- 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:
- The ViewModel is a non-visual class and does not derive from any WPF or Silverlight base class.
- It encapsulates the presentation logic required to support a use case or user task in the application.
- The ViewModel is testable independently of the view and the model.
- 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.
- It implements properties and commands to which the view can data bind.
- It notifies the view of any state changes via change notification events via the
INotifyPropertyChanged
andINotifyCollectionChanged
interfaces. - Interacts with View with various Commands.
- The view model coordinates the view's interaction with the model.
- 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.
- It may also implement data validation via the
IDataErrorInfo
orINotifyDataErrorInfo
interfaces. - The view model may define logical states that the view can represent visually to the user.
- Invokes services to communicate outside the MVVM triad.
Source: http://code.msdn.microsoft.com/Design-Patterns-MVVM-Model-d4b512f0
这篇关于WPF的MVVM的简单定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!