zoukankan      html  css  js  c++  java
  • 无废话WPF系列19:MVVM简单介绍

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

    image

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

    image

    XAML代码

    <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

     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); }
            }
    
        }
     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;
            }
        }

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

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                MainPageViewModel mainPageViewModel = new MainPageViewModel { Age = 20, Name = "Jack" };
                this.DataContext = mainPageViewModel;
            }      
        }
  • 相关阅读:
    (山理工 1243)母牛问题(大牛生小牛,小牛生。。。。)
    Linux内核分析——第五周学习笔记
    《Linux内核设计与实现》第五章读书笔记
    day41-死锁,信号量,线程q,协程
    day40-线程相关
    day39-进程,线程相关知识
    day38-并发编程
    day37-套接字后续
    day36-socket(C/S架构)套接字
    day35-网络剩余
  • 原文地址:https://www.cnblogs.com/cnblogsfans/p/1967858.html
Copyright © 2011-2022 走看看