1. MVVM
MVVM的设计模式最早于2005年由微软的WPF和Silverlight架构师John Gossman在他的博客中提到。
WPF中采用MVVM的架构可以获得以下好处:
1. 将UI和业务的设计完全分开,View只是ViewModel的消费者
2. 有助于我们区别并哪些是UI操作,哪些是业务操作,而不是将他们混淆
3.层与层之间耦合度降低,这一点非常符合面向对象(OOP)的思想。
2.MVVM 用图来表示,这个是从网上找的图,简单明了,省去了自己画。
3.下面来一步一步写代码吧!
3.1 在项目根目录创建Model文件夹,并新增一个实体类,PersonModel,实现INotifyPropertyChanged通知接口。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MVVMDemo.Model
{
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private string name = "吃饭了";
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
} private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null) return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} public void Show(object o)
{
this.Name += ",吃饭了"; }
}
}
PersonModel
3.2 在项目根目录创建ViewModel文件夹,并新增一个PersonViewModel
using MVVMDemo.Commands;
using MVVMDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MVVMDemo.ViewModel
{
public class PersonViewModel
{
public PersonCommand PersonCommand { get; set; }
public Person PersonModel { get; set; } public PersonViewModel()
{
this.PersonModel = new Person();
this.PersonCommand = new PersonCommand();
this.PersonCommand.ExecuteCommand = this.PersonModel.Show; } }
}
PersonViewModel
3.3 在项目根目录创建Commands文件夹,并新增一个PersonCommand,实现ICommand接口。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input; namespace MVVMDemo.Commands
{
public class PersonCommand : ICommand
{
public Func<object, bool> CanExecuteCommand = null;
public Action<object> ExecuteCommand = null; public bool CanExecute(object parameter)
{
if (CanExecuteCommand == null) return true;
return CanExecuteCommand(parameter);
} public event EventHandler CanExecuteChanged; public void Execute(object parameter)
{
if (ExecuteCommand == null) return;
ExecuteCommand(parameter);
} public void OnCanExecuteChanged()
{
if (CanExecuteChanged == null) return;
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
PersonCommand
3.4 MainWindow.xaml调用
1 <Window x:Class="MVVMDemo.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="MainWindow" Height="434.432" Width="607.471">
5 <Grid Margin="0,0,2,5">
6
7 <TextBlock Name="lbShow" Text="{Binding PersonModel.Name}" Margin="10,274,-10,10" />
8 <Button Command="{Binding PersonCommand}" Height="20" Width="120" Margin="214,211,214,93" Content="MVVM"></Button>
9 <!-- <Button Height="20" Width="120" Content="{Binding ElementName=gb,Path=Value,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"></Button> -->
10 </Grid>
11 </Window>
using MVVMDemo.ViewModel;
using System.Windows; namespace MVVMDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new PersonViewModel(); }
}
}
3.5测试结果
很简单的例子,希望能看懂,这是基础。