《深入浅出WPF》学习笔记六.手动实现Mvvm

server/2024/10/21 10:20:31/

《深入浅出WPF》学习笔记六.手动实现Mvvm

demo的层级结构,Mvvm常用项目结构

依赖属性基类实现

具体底层原理后续学习中再探讨,可以粗浅理解为,有一个全局对象使用list或者dic监听所有依赖属性,当一个依赖属性变化引发通知时,就会遍历查询对应的字典,通知View层进行处理。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;namespace WpfApp11.ViewModels
{/// <summary>/// viewmodel的基类/// </summary>public class NotificationObject : INotifyPropertyChanged{/// <summary>/// 通知属性变化/// </summary>public event PropertyChangedEventHandler? PropertyChanged;public void RaisePropertyChanged(string propertyName){if (this.PropertyChanged != null){//实际方法,内部可以理解为一个字典对象this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));}}}
}

命令属性基类实现

这个底层原理没了解过,后续学到了再说。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace WpfApp11.Commands
{public class DelegateCommand : ICommand{public event EventHandler? CanExecuteChanged;//是否可以执行public bool CanExecute(object? parameter){if (this.CanExecuteFunc == null){return true;}else{this.CanExecuteFunc(parameter);return true;}}//具体执行public void Execute(object? parameter){if (this.ExecuteAction == null){return;}this.ExecuteAction(parameter);}public Action<object> ExecuteAction { get; set; }public Func<object, bool> CanExecuteFunc { get; set; }}
}

View

<Window x:Class="WpfApp11.Views.CalculateView"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:WpfApp11.Views"mc:Ignorable="d"Title="CalculateView" Height="300" Width="300"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"></RowDefinition><RowDefinition Height="auto"></RowDefinition><RowDefinition Height="auto"></RowDefinition><RowDefinition Height="auto"></RowDefinition></Grid.RowDefinitions><StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10"><Label>参数1:</Label><TextBox Height="30" Width="100"  x:Name="tb1"  Text="{Binding Param1}"></TextBox></StackPanel><StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10"><Label>参数2:</Label><TextBox Height="30" Width="100"  x:Name="tb2" Text="{Binding Param2}"></TextBox></StackPanel><StackPanel Grid.Row="2" Orientation="Horizontal" Margin="10"><Label>结果:</Label><TextBox Height="30" Width="100"  x:Name="tb3" Text="{Binding Result}"></TextBox></StackPanel><StackPanel Grid.Row="3" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10"><Button x:Name="btn1" Content="Calculate" Height="30" Width="100" Command="{Binding AddCommand}"></Button></StackPanel></Grid>
</Window>
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.Shapes;
using WpfApp11.ViewModels;namespace WpfApp11.Views
{/// <summary>/// CalculateView.xaml 的交互逻辑/// </summary>public partial class CalculateView : Window{public CalculateView(){InitializeComponent();this.DataContext = new CalculateViewModel();}}
}

ViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp11.Commands;namespace WpfApp11.ViewModels
{public class CalculateViewModel : NotificationObject{#region 依赖属性private string param1;public string Param1{get { return param1; }set{param1 = value;this.RaisePropertyChanged("Param1");}}private string param2;public string Param2{get { return param2; }set{param2 = value;this.RaisePropertyChanged("Param2");}}private string result;public string Result{get { return result; }set{result = value;this.RaisePropertyChanged("Result");}}#endregion/// <summary>/// 命令属性/// </summary>public DelegateCommand AddCommand {  get; set; }public void Add(object param){this.Result = (Convert.ToInt32(this.Param1) + Convert.ToInt32(this.Param2)).ToString();}public CalculateViewModel(){Param1 = "0";Param2 = "0";Result = "0";this.AddCommand = new DelegateCommand();this.AddCommand.ExecuteAction = new Action<object>(this.Add);}}
}

App

<Application x:Class="WpfApp11.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp11"StartupUri="Views/CalculateView.xaml"><Application.Resources></Application.Resources>
</Application>

结语

实际项目中基本都是使用成熟框架Prism或者MvvmLight,不需要手动造轮子,但学习要知所以然。


http://www.ppmy.cn/server/95813.html

相关文章

135. 分发糖果【 力扣(LeetCode) 】

一、题目描述 n 个孩子站成一排。给你一个整数数组 ratings 表示每个孩子的评分。 你需要按照以下要求&#xff0c;给这些孩子分发糖果&#xff1a; 每个孩子至少分配到 1 个糖果。相邻两个孩子评分更高的孩子会获得更多的糖果。 请你给每个孩子分发糖果&#xff0c;计算并返回…

hive自动安装脚本

使用该脚本注意事项 安装hive之前确定机子有网络。或者yum 更改为本地源&#xff0c;因为会使用epel仓库下载一个pv的软件使用该脚本前提是自行安装好mysql数据库准备好tomcat软件包&#xff0c;该脚本使用tomcat9.x版本测试过能正常执行安装成功&#xff0c;其他版本没有测试…

打造未来交互新篇章:基于AI大模型的实时交互式流媒体数字人项目

在当今数字化浪潮中,人工智能(AI)正以前所未有的速度重塑我们的交互体验。本文将深入探讨一项前沿技术——基于AI大模型的实时交互式流媒体数字人项目,该项目不仅集成了多种先进数字人模型,还融合了声音克隆、音视频同步对话、自然打断机制及全身视频拼接等前沿功能,为用…

【STM32】“stm32f10x.h” 头文件的作用

目录 1. 文件结构与头文件保护1.1 头文件保护1.2 包含的头文件 2. 宏定义和常量2.1 系统时钟相关2.2 外设时钟使能2.3 中断优先级 3. 外设寄存器结构体3.1 GPIO 寄存器结构体3.2 RCC 寄存器结构体3.3 USART 寄存器结构体 4. 外设头文件的引入4.1 GPIO 外设头文件4.2 RCC 外设头…

【前端element-ui】对于封装el-select和checkbox-group的多选控件导致数据双向绑定失败问题的处理

一、关于通过父组件props传参el-select【下拉多选模式】双向绑定失败问题处理方式 1、this.$emit(“change”, val); 采用change二不是input 2、对_value赋值不能直接使用""号&#xff0c;而是push <template><div><el-select v-model"_value&q…

警惕!过度焦虑背后的隐形伤害:你的身体在悄悄发出警告

在这个快节奏、高压力的时代&#xff0c;焦虑似乎成了现代人难以逃脱的情绪枷锁。偶尔的焦虑感或许能激发我们的动力&#xff0c;促使我们面对挑战&#xff0c;但当这种情绪超出可控范围&#xff0c;演变为过度焦虑时&#xff0c;它不仅侵蚀着我们的心理健康&#xff0c;更在悄…

ShardingSphere 之ShardingJDBC扩展功能:分片审计、数据加密、读写分离、广播表、绑定表

文章目录 分片审计数据加密读写分离广播表绑定表 分片审计 开发者手册 分片审计功能是针对数据库分片场景下对执行的 SQL 语句进行审计操作。可以对我们要执行的sql进行拦截并进行审核。 目前ShardingSphere内置的分片审计算法只有一个&#xff0c;DML_SHARDING_CONDITIONS。…

使用openpyxl库对excel函数处理数据

哈喽,大家好,我是木头左! 在本文将介绍如何使用openpyxl库对Excel文件进行函数处理,包括使用内置函数、自定义函数和引用其他工作表的函数等。 使用内置函数 openpyxl提供了许多内置函数,可以直接在单元格中使用。例如,可以使用SUM函数计算一列或一行的总和,使用AVERA…