MVVM即Module-View-ViewModule的简称,是MVC进化发展而来的。
module是现实世界某个实体或某个抽象实体的数字化表示。比如实体人可以是个module,这个module有name,age,gender,score等属性,只要你程序处理需要,你就可以将某个属性添加进module中。比如抽象实体,一个地方的气象信息可以是个module,这个module有place,time ,temperature,humidity等属性。不同实体有不同的module,同样的实体也可以有不同module,比如人,学生和职员所要关注的属性很不同,就具有不同的module。
view就是视图,人眼可以看到的button、text等组成的control布局堆列。
ViewModule是module和view的中间类型,它封装了module里的属性,又保存了一些行为。并使其跟界面关联,以响应界面的更改。
viewmodule跟view是通过binding关联的起来的。binding是种关系说明,可以理解为关联。
Module:
namespace WpfTest {
public class PersonModule {
public string Name { get; set; }
public int Age { get; set; }
public PersonModule() {
Name = "";
Age = 0;
}
}
}
View:
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfTest="clr-namespace:WpfTest"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox Grid.Column="1" Grid.Row="1" Margin="20" Background="BurlyWood" Text="{Binding Path=Name}"/>
<TextBox Grid.Column="1" Grid.Row="2" Margin="20" Background="BurlyWood" Text="{Binding Path=Age}"/>
<Button Grid.Row="3" Margin="20,30,20,10" Content="确定" Grid.Column="1" Command="{Binding Path=TestCmd}"></Button>
</Grid>
</Window>
ViewModule:
namespace WpfTest {
public class PersonViewModule : INotifyPropertyChanged {
private PersonModule m_person;
public string Name {
get {
return m_person.Name;
}
set {
m_person.Name = value;
OnPropertyChanged("Name");
}
}
public int Age {
get {
return m_person.Age;
}
set {
m_person.Age = value;
OnPropertyChanged("Age");
}
}
public ICommand TestCmd {
get {
return new myRelayCommand(() => {
MessageBox.Show("Name:" + m_person.Name + "\n" + "Age:" + m_person.Age);
});
}
}
public PersonViewModule() {
m_person = new PersonModule();
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
myRelayCommand类:
namespace WpfTest {
public class myRelayCommand:ICommand {
public myRelayCommand(Action execute) : this(execute, null) {}
public Action m_execute;
public Func<bool> m_canExecute;
public myRelayCommand(Action execute, Func<bool> canExecute) {
if (execute == null) {
throw new ArgumentException("execute");
}
m_execute = execute;
m_canExecute = canExecute;
}
public bool CanExecute(object parameter) {
return m_canExecute == null ? true : m_canExecute();
}
public void Execute(object parameter) {
m_execute();
}
public event EventHandler CanExecuteChanged {
add {
if (m_canExecute != null) {
CommandManager.RequerySuggested += value;
}
}
remove {
if (m_canExecute != null) {
CommandManager.RequerySuggested -= value;
}
}
}
}
}