博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一段关于MVVM的英文释义
阅读量:5072 次
发布时间:2019-06-12

本文共 3590 字,大约阅读时间需要 11 分钟。

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) .

实例
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

你可能感兴趣的文章