WPF MVVM实例三

在没给大家讲解wpf mwm示例之前先给大家简单说下MVVM理论知识:

WPF技术的主要特点是数据驱动UI,所以在使用WPF技术开发的过程中是以数据为核心的,WPF提供了数据绑定机制,当数据发生变化时,WPF会自动发出通知去更新UI。

  我们使用模式,一般是想达到高内聚低耦合。在WPF开发中,经典的编程模式是MVVM,是为WPF量身定做的模式,该模式充分利用了WPF的数据绑定机制,最大限度地降低了Xmal文件和CS文件的耦合度,也就是UI显示和逻辑代码的耦合度,如需要更换界面时,逻辑代码修改很少,甚至不用修改。与WinForm开发相比,我们一般在后置代码中会使用控件的名字来操作控件的属性来更新UI,而在WPF中通常是通过数据绑定来更新UI;在响应用户操作上,WinForm是通过控件的事件来处理,而WPF可以使用命令绑定的方式来处理,耦合度将降低。

首先MVVM设计模式的结构

27e1c2fe8589f27667f813e6a8f8e84b.png

Views: 由Window/Page/UserControl等构成,通过DataBinding与ViewModels建立关联;
ViewModels:由一组命令,可以绑定的属性,操作逻辑构成;因为View与ViewModel进行了解耦,我们可以对ViewModel进行Unit Test;

Models:可以是实体对象或者Web服务;
下面通过一个简单的例子,来介绍一些WPF MVVM模式。首先项目结构:

e84df30d907913dce9592b1ef9ce58f6.png

DelegateCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp13.Commands;namespace WpfApp13.ViewModels
{class MainWindowViewModel:NotificationObject{private double  input1;public double Input1{get { return input1; }set{input1 = value;this.RaisePropertyChange("Input1");}}private double input2;public double Input2{get { return input2; }set{input2 = value;this.RaisePropertyChange("Input2");}}private double result;public double Result{get { return result; }set{result = value;this.RaisePropertyChange("Result");}}public DelegateCommand AddCommand { get; set; }public void Add(object parameter){this.Result = this.Input1 + this.Input2;}public MainWindowViewModel(){this.AddCommand = new DelegateCommand();this.AddCommand.ExcuteAction = new Action<object>(this.Add);}}
}

MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp13.Commands;namespace WpfApp13.ViewModels
{class MainWindowViewModel:NotificationObject{private double  input1;public double Input1{get { return input1; }set{input1 = value;this.RaisePropertyChange("Input1");}}private double input2;public double Input2{get { return input2; }set{input2 = value;this.RaisePropertyChange("Input2");}}private double result;public double Result{get { return result; }set{result = value;this.RaisePropertyChange("Result");}}public DelegateCommand AddCommand { get; set; }public void Add(object parameter){this.Result = this.Input1 + this.Input2;}public MainWindowViewModel(){this.AddCommand = new DelegateCommand();this.AddCommand.ExcuteAction = new Action<object>(this.Add);}}
}

NotificationObject.CS

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WpfApp13.ViewModels
{class NotificationObject : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void RaisePropertyChange(string propertyName){if(this.PropertyChanged!=null){this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));}}}
}

MainWindow.xaml.CS

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;
using WpfApp13.ViewModels;namespace WpfApp13
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainWindowViewModel();}}}

MainWindow.xaml

<Window x:Class="WpfApp13.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:WpfApp13"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><Slider Name="slider1" MinHeight="25" Value="{Binding Input1}"/><Slider Name="slider2" MinHeight="25" Value="{Binding Input2}"/><Slider Name="slider3" MinHeight="25" Value="{Binding Result}"/><Button Name="addButton" Content="ADD" FontSize="30" MinHeight="40" Command="{Binding AddCommand}"/></StackPanel>     </Grid>
</Window>

运行效果:

分别拖动滑块slider1和slider2,点击按钮后slider3就会自动变化

ab23b7fc158da4881d068a02f91543d9.png

百度网盘源码下载地址:

链接:https://pan.baidu.com/s/1AvBncokY8SiW0fd9XqrPRw 

提取码:ttpo 

技术群: 需要进技术群学习交流的请添加小编微信,切记备注:加群,对以上内容有什么疑问也可以直接和小编直接沟通交流!     

小编微信:mm1552923   

公众号:dotNet编程大全   

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/293145.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux命令chown和chmod什么区别

chown一般用来 更改属主。也就是文件所属用户。chmod功能要比chown要强大。可更改文件所有属性和权限。只有管理员账户才有权限用此命令。chown 是修改文件的所有者(owner),和所属组(group)chmod 是修改文件的执行属性(所属组,所属者以及其他人所有的权限,比如 读,写,执行)

stringcstdlibctimecstdargctimectypecmathclimits

转载地址&#xff1a;http://blog.csdn.net/kz_ang/article/details/7767335 <string>头文件 string构造函数 string s  生成一个空字符串s string s(str)  拷贝构造函数,生成str对象的复制品 string s(str,stridx)  将字符串str对象内"始于位置stridx"…

背包模板

背包模板&#xff0c;自己总结&#xff0c;做题可直接套用。 0-1背包 有N件物品和一个容量为V的背包。第i件物品的费用是c[i]&#xff0c;价值是w[i]。求解将哪些物品装入背包可使价值总和最大。 公式&#xff1a; f[i][v]max{f[i-1][v],f[i-1][v-c[i]]w[i]} 伪代码&#xff1a…

C3P0 释放连接 的问题

2019独角兽企业重金招聘Python工程师标准>>> 记录一下使用C3P0 时犯的低级错误&#xff0c;没有关闭connection 达到连接池最大限制后造成程序假死的现象&#xff0c;以后得仔细再仔细了&#xff01; 转载于:https://my.oschina.net/fusxian/blog/146700

python随机抽取人名_python的random

python的random函数更多的random用法可参阅&#xff1a;random --- 生成伪随机数 - Python 3.7.4 文档​docs.python.org以下使用了&#xff1a;洗牌&#xff1a;random.shuffle随机抽取元素&#xff0c;且元素不重复&#xff1a;random.sample随机抽取元素&#xff0c;且元素可…

PL/SQL Developer使用技巧总结

1、PL/SQL Developer记住登陆密码 在使用PL/SQL Developer时&#xff0c;为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码&#xff1b; 设置方法&#xff1a;PL/SQL Developer 7.1.2 ->tools->Preferences->Oracle->Logon History &#xff0c; “…

Windows 11/10 正式版全新 ISO 镜像下载

微软现已发布 Windows 11/10 正式版&#xff08;版本 21H2&#xff09;全新 ISO 镜像。Windows 11 版本 21H2 全新镜像集成了 2021 年 10 月更新、11 月更新和 12 月更新。Windows 10 版本 21H2 全新镜像集成了 2021 年 11 月更新和 12 月更新。版本区别Windows 11/10 消费者版…

Android延时执行调用的几种方法

一、开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主线程执行任务 } }).start二、利用定时器 TimerTask task new TimerTask(){ public void run(){ //execute the task } }; Ti…

删除字符串中出现次数最少的字符

题目 描述: 实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。 题目类别: 字符串 难度: 中级 运行时间限制: 10Sec 内存限制: 128MByte 阶段: 入职前练习 输入: 字符串只包含小写…

java web filter 之一 基础实现

本文主要对filter的基本使用进行了讲解&#xff0c;其中涉及到了 filter是什么 一个filter处理一个jsp 多个filter处理一个jsp filter是什么 Filter 是java下的一种过滤器 &#xff0c;能实现对java web程序 客户端和服务器端消息的过滤&#xff0c;也就是在服务器段接受reques…

eclipse索引4超出范围_Python内置的4个重要基本数据结构:列表、元组、字典和集合

本章内容提要&#xff1a;列表及操作元组及操作字典及操作集合简介第2章介绍了数字(整数、浮点数)、逻辑值和字符串等Python内置的基本数据类型。在实际的操作中&#xff0c;仅仅依赖它们很难高效地完成复杂的数据处理任务。基于对基本数据类型的构建&#xff0c;Python拓展出列…

.NET 6 中 System.Text.Json 的新特性

1支持忽略循环引用在 .NET 6 之前&#xff0c;如果 System.Text.Json 检测到循环引用&#xff0c;就会抛出 JsonException 异常。在 .NET 6 中&#xff0c;你可以忽略它们。Category dotnet new() {Name ".NET 6", }; Category systemTextJson new() {Name "…

Java基础知识回顾--线程

基本概念 线程是一个程序内部的顺序控制流 Java的线程是通过java.lang.Thread类来实现的。main函数是一个主线程&#xff0c;用户可以通过创建Thread的实例来创建新的线程。每一个线程都必须实现run方法。通过Thread类的start方法来启动一个线程。 两种方式实现&#xff0c;一…

读书笔记:季羡林关于如何做研究学问的心得

以下的内容摘自&#xff1a; 季羡林 (2010-02-01). 牛棚杂忆(季羡林作品珍藏本)(图文版) (Kindle Locations 2701-2706). . Kindle Edition. 我记得&#xff0c;鲁迅先生在一篇文章中讲了一个笑话&#xff1a;一个江湖郎中在市集上大声吆喝&#xff0c;叫卖治臭虫的妙方。有人…

四、物理优化(1)范式化

一、范式化的概念范式化&#xff08;Normalization&#xff09;的目标是消除数据冗余&#xff0c;减小物理存储空间。常用的范式有以下3种&#xff1a;1. 第一范式&#xff08;1NF&#xff09;在任何一个关系数据库中&#xff0c;1NF是对关系模式的基本要求&#xff0c;不满足1…

​如何在图书馆收获一段爱情?​

1 你为什么不喜欢拍照片&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼2 当登记处挂起横幅▼3 这几天老是犯的错误▼4 在图书馆收获的爱情&#xff08;素材来源网络&#xff0c;侵删&#xff09;▼5 当老师用键盘讲课&#xff08;素材来源网络&#xff0c;侵删&a…

坐标移动

题目 开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。 输入: 合法坐标为A(或者D或者W或者S) + 数字(两位以内) 坐标之间以;分隔。 非…

MongoDB服务无法启动,原因居然是...

前言某台CentOS服务器上的MongoDB服务突然停止运行&#xff0c;于是登上去排查原因&#xff0c;接下来就是苦逼的爬坑之旅。爬坑记录1、重启服务使用systemctl命令重启服务&#xff1a;systemctl restart mongod.service查看系统日志&#xff1a;journalctl -ex只是提示启动失败…