一段关于MVVM的英文释义

本文深入解析MVVM模式,包括其核心组件如模型、视图和视图模型,以及关键特性如INotifyPropertyChanged接口和ICommand接口在WPF和Silverlight中的应用。同时,展示了如何通过数据模板实现动态视图渲染,并提供了具体的实例代码。

MVVM

The MVVM pattern is an evolution of the PM pattern that has the three usual principal components:

a Model that represents the business entity (like the Employee class example),

a View that is the XAML UI,

the PM or View Model, which contains all the UI logic and the reference to the Model, so it acts as the Model for the View .

ViewModel和WPF,Silverlight的定制

One of these is the INotifyPropertyChanged interface, introduced with .NET Framework version 2 .0 . This interface implements a notification system that activates when the value of a property changes . It’s required in the ViewModel to make the binding engine of XAML work properly .

Another customization of the PM is the Command exposed by the interface ICommand, which is available for WPF and Silverlight . This specific Command can be bound to any XAML control and determines whether the control can or cannot execute a specific action . In WPF, this Command has a more powerful implementation through the Routed Command, which is a Command routed through the Visual Tree of a WPF UI .

A third customizable component is the DataTemplate, an XAML structure that defines how to render a specific ViewModel, or a specific state of the ViewModel . DataTemplate components are really views that are rendered at runtime by the WPF/Silverlight engine . They are particular type of Views that cannot contain any code behind because they are dynamically created .

Logically you are displaying a ViewModel or Model directly in the UI, but the view is conjured up at runtime and attached to the ViewModel or Model (through the data context) .

clip_image004[4]

实例
Model

同Employee

ViewModel

/// <summary>

/// ViewModel for the Employee view

/// </summary>

public sealed class EmployeeViewModel : INotifyPropertyChanged

{

publicEmployeeViewModel()

{

var employee = new Employee

{

FirstName = "John",

LastName = "Smith",

Company = "Microsoft"

};

//Bind the model to the viewmodel

this.Firstname = employee.FirstName;

this.Lastname = employee.LastName;

this.Company = employee.Company;

}

#region INotifyPropertyChanged

/// <summary>

/// Occurs when a property value changes.

/// </summary>

public event PropertyChangedEventHandlerPropertyChanged;

/// <summary>

/// Called when [property changed].

/// </summary>

/// <param name="name">The name.</param>

public void OnPropertyChanged(string name)

{

var handler = PropertyChanged;

if (handler != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(name));

}

}

#endregion

/// <summary>

/// Private accessor for the Firstname

/// </summary>

private string firstname;

/// <summary>

/// Gets or sets the firstname.

/// </summary>

/// <value>The firstname.</value>

public string Firstname {

get

{

returnfirstname;

}

set

{

if (firstname != value)

{

firstname = value;

OnPropertyChanged("Firstname");

}

}

}

// omitted

}

View

<Window.DataContext>

<vm:EmployeeViewModel />

</Window.DataContext>

<StackPanel Orientation="Vertical">

<TextBlock>FirstName :</TextBlock>

<TextBox

Text="{Binding Path=Firstname,

Mode=TwoWay,

UpdateSourceTrigger=PropertyChanged}" />

<TextBlock>Lastname :</TextBlock>

<TextBox

Text="{Binding Path=Lastname,

Mode=TwoWay,

UpdateSourceTrigger=PropertyChanged}" />

<TextBlock>Company :</TextBlock>

<TextBox

Text="{Binding Path=Company,

Mode=TwoWay,

UpdateSourceTrigger=PropertyChanged}" />

</StackPanel>

优缺点

the MVVM pattern is designed for use with WPF or Silverlight

one key advantage of adopting the MVVM pattern is that the View is an observer of theViewModel, which makes it easier to build the UI separately

 
 

转载于:https://www.cnblogs.com/junbird-nest/archive/2012/03/10/2389390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值