WPF基础学习&MVVM例子讲解
创作时间:
作者:
@小白创作中心
WPF基础学习&MVVM例子讲解
引用
CSDN
1.
https://m.blog.csdn.net/qq_41714549/article/details/145574047
本文是一篇关于WPF(Windows Presentation Foundation)基础学习和MVVM(Model-View-ViewModel)模式讲解的技术文章。文章详细介绍了NotificationObject、DelegateCommand等核心概念,并通过一个具体的加法计算器案例展示了View与ViewModel的交互过程。文章结构清晰,内容完整,包含代码示例和运行效果,适合有一定C#基础的开发者阅读。
前言
参考文章:
参考视频:《深入浅出WPF》系列高清视频教程 | 讲师:刘铁猛
自己的感想
一、NotificationObject与数据属性
创建个类,声明NotificationObject
用于双向的数据绑定
internal class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChange(string propertyName)
{
if(this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
PropertyChanged 事件:订阅者(如 UI 控件)通过监听此事件感知属性变化。
RaisePropertyChange 方法:手动触发事件,传递发生变更的属性名。
上面的RaisePropertyChange()方法可以优化:
public void RaisePropertyChange(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
二、DelegateCommand与命令属性
做操作传输的命令属性命名为DelegateCommand
CanExecute
决定命令是否可用
Execute
执行命令的实际逻辑
CanExecuteChanged
在命令的可执行状态变化时通知UI更新。
internal class DelegateCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
if (this.CanExecuteFunc == null)
{
return true;
}
return this.CanExecuteFunc(parameter);
}
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与ViewModel的交互(难点)
NotificaitonObject.cs是ViewModel文件夹中类的基类。里面的其他类都要继承NotificationObject.cs类。
在ViewModel文件下创建MainWindowViewModel
internal 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"); }
}
//1个方法
public DelegateCommand AddCommand { get; set; }
private void Add(Object parameter)
{
this.Result = this.Input1 + this.Input2;
}
public MainWindowViewModel()
{
this.AddCommand = new DelegateCommand();
this.AddCommand.ExecuteAction = new Action<object>(this.Add);
}
}
数据和方法绑定
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button x:Name="btnSave" Content="Save"/>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox x:Name="slider1" Grid.Row="0" FontSize="24" Margin="4"
Background="LightBlue"
Text="{Binding Input1}"/>
<TextBox x:Name="slider2" Grid.Row="1" FontSize="24" Margin="4"
Background="LightBlue"
Text="{Binding Input2}"/>
<TextBox x:Name="slider3" Grid.Row="2" FontSize="24" Margin="4"
Background="LightBlue"
Text="{Binding Result}"/>
<Button Name="addBtn" Grid.Row="3" Content="Add" Width="120" Height="80" Command="{Binding AddCommand}" />
</Grid>
</Grid>
资源指定
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
四、优势体现
这种框架的优势体现在:逻辑功能不变的情况下,前端怎么改都可以很快实现功能,只要映射回原来的方法和属性即可。
下面就是直接将前端UI代码拷贝进来,后端不变,效果依旧是result = input1+input2.
热门推荐
年糕怎么保存 年糕变硬如何回软
关税风云录:从南北战火到中美贸易波澜的历史回响
硫酸新霉素的作用功效
醋酸氯己定的功效和作用是什么
装饰材料行业色彩一致性的管理解决方案
痘痘的起因曝光!神经酰胺是最佳解决方案?挑战!30天「神经酰胺与抗痘」保养计划
自制天然去污粉的方法:马桶的黄渍、霉菌、水垢全部消失
室内绿植养护指南:打造绿色健康生活空间
胳膊剧烈疼痛竟是颈椎惹的祸,微创手术巧解压迫神经的“大山”
宋朝皇室对宗室女的婚姻有何规定?宋代婚嫁制度有何变化?
《赛马娘 热血喧闹大感谢祭!》评测 门槛十分低的轻松派对游戏
移动得更好,用侧弓步避开伤害
脸上长了痘挤了就能好?正确处理方法在这里
被AlphaGo击败的李世石,用8年重建崩塌的世界
高中数学备考攻略:必练题型盘点及高效刷题推荐
脾切除手术后可能出现的五大后遗症及预防措施
脾切除手术后的饮食护理指南
9200 万独居青年的深夜觉醒:从 “生存性孤独“ 到 “创造性独处
全球视野下的公司名称注册:步骤、注意事项与各国要求全解析
汉式婚礼:从炎黄到明末的四千年传承
盘点植物大战僵尸各大版本,你都玩过几个?
二战末期的"虎王":一场被遗忘的装甲传奇
糖尿病饮食指南:低GI水果清单与科学搭配建议
【科普帖】大家平时睡的枕头都选对了吗?——枕头应如何选择
魏纯暹新作《艺术城市》:以艺术视角探索城市发展的未来之路
地铁站装修效果图设计:塑造城市文化与现代交通的新地标
交通强国·试点先行 | 这样做让交通更加“智慧”
新能源汽车最大载重375公斤引发争议
日本学院动漫:从经典到新兴的发展历程
从 "兵仙" 到 "反贼":韩信的悲剧人生与帝王权术的博弈