代码改变世界

无废话WPF系列19:MVVM简单介绍

  敏捷的水  阅读(1628)  评论(3编辑  收藏  举报

MVVM主要是为了逻辑代码和视图的分离,使CodeBehind只包含对UI的操作。通过绑定和Command来实现

image

下面我们实现一个最简单的示例,点击按钮使年龄加1.

image

XAML代码

1
2
3
4
5
6
7
8
9
10
11
12
13
<Window x:Class="DeepXAML.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DeepXAML"      
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:cl="clr-namespace:System.Collections;assembly=mscorlib"
        Title="MainWindow" Height="250" Width="450">
       <StackPanel>
        <TextBox Text="{Binding Path=Name}"></TextBox>
        <TextBox Text="{Binding Path=Age}"></TextBox>
        <Button Command="{Binding Path=AddAge}" >Add Age</Button>
    </StackPanel>
</Window>

 

MainPageViewModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class MainPageViewModel : INotifyPropertyChanged
   {
       public event PropertyChangedEventHandler PropertyChanged;
 
       private string name;
       public string Name {
           get { return name; }
           set {
               name = value;
               if (this.PropertyChanged != null)
               {
                   this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
               }
           }
       }
 
       private int age;
       public int Age {
           get { return age; }
           set
           {
               age = value;
               if (this.PropertyChanged != null)
               {
                   this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
               }
           }
       }
 
       public ICommand AddAge
       {
           get { return new AddAgeCommand(this); }
       }
 
   }

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class AddAgeCommand : ICommand
   {
       private MainPageViewModel mMainPageViewModel;
       public AddAgeCommand(MainPageViewModel model)
       {
           mMainPageViewModel = model;
       }
 
       public bool CanExecute(object parameter)
       {
           return true;
           
       }
 
       public event EventHandler CanExecuteChanged;
 
       public void Execute(object parameter)
       {
           this.mMainPageViewModel.Age += 1;
       }
   }

 

我们可以看一下后台只有很少代码:

1
2
3
4
5
6
7
8
9
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MainPageViewModel mainPageViewModel = new MainPageViewModel { Age = 20, Name = "Jack" };
            this.DataContext = mainPageViewModel;
        }     
    }
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2010-03-01 CruiseControl.NET with svn over SSH
点击右上角即可分享
微信分享提示