一、MVVM介紹
MVVM是Model-View-ViewModel(模型-視圖-視圖模型)的縮寫形式
1、View就是用xaml實現的界面,負責與用戶交互,接收用戶輸入,把數據展現給用戶。
2、ViewModel是一個C#類,負責收集需要綁定的數據和命令,聚合Model對象,通過View類的DataContext屬性綁定到View,同時也可以處理一些UI邏輯。
3、Model,就是系統中的對象,可包含屬性和行為。
三者之間的關系:View對應一個ViewModel,ViewModel可以聚合N個Model,ViewModel可以對應多個View
二、MVVM的優勢
三權分立。另外,和面向接口編程的思想有點相同;也和 asp.net mvc 有點像。
三、MVVM簡單示例
1.項目結構
我個人喜歡先寫 Model,再寫 ViewModel, 最后在 xaml 里面綁定數據
Model: UserInfo
代碼:

using System; using System.Collections.Generic; using System.Text; namespace WpfMVVMDemo.Model { public class UserInfo { private String _Name; private int _Age; public string Name { get => _Name; set => _Name = value; } public int Age { get => _Age; set => _Age = value; } } }

using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using WpfMVVMDemo.Model; namespace WpfMVVMDemo { public class UserInfoViewModel : INotifyPropertyChanged { private UserInfo userInfo; public UserInfoViewModel() { this.userInfo = new UserInfo() { Name = "張三", Age = 18 }; } public String Name { get { return this.userInfo.Name; } set { this.userInfo.Name = value; RaisePropertyChanged("Name"); } } public int Age { get { return this.userInfo.Age; } set { this.userInfo.Age = value; RaisePropertyChanged("Age"); } } public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(String propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } protected virtual void SetAndNotifyIfChanged<T>(String propertyName, ref T oldVal, ref T newVal) { if (oldVal == null && newVal == null) return; if (oldVal != null && oldVal.Equals(newVal)) return; if (newVal != null && newVal.Equals(oldVal)) return; oldVal = newVal; RaisePropertyChanged(propertyName); } } }
在 MainWindow.xaml 中 聲明一個 ViewModel,就像 asp.net mvc 從后端把數據傳給視圖一樣。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfMVVMDemo { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { UserInfoViewModel viewModel = new UserInfoViewModel(); public MainWindow() { InitializeComponent(); this.viewModel = base.DataContext as UserInfoViewModel; } } }
修改 xaml:

<Window x:Class="WpfMVVMDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfMVVMDemo" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.DataContext> <local:UserInfoViewModel></local:UserInfoViewModel> </Window.DataContext> <Grid> <TextBox Text="{Binding Name}" HorizontalAlignment="Left" Margin="176,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Label Content="姓名" HorizontalAlignment="Left" Margin="122,78,0,0" VerticalAlignment="Top"/> <TextBox Text="{Binding Age}" HorizontalAlignment="Left" Margin="176,112,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Label Content="年齡" HorizontalAlignment="Left" Margin="122,108,0,0" VerticalAlignment="Top"/> </Grid> </Window>
Done!
最近在了解工控方面的編程,發現WPF搞工控開發很好。
QQ:77915862
有興趣的可以一起交流。