微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言

如果对您有所帮助:欢迎赞赏

简化MVVM属性设置和修改 - .NET CORE(C#) WPF开发

阅读导航

  1. 常用类属性设置、获取方式
  2. 二次封装 INotifyPropertyChanged
  3. Demo 展示、源码下载

1. 常用类属性设置、获取方式

public class Student : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
if(name != value)
{
name = value;
OnPropertyChanged("Name")
}
}
} #region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}

这种方式总感觉有点啰嗦,如果 Name 属性名修改,字符串 "Name" 还要手动修改,就算 Ctrl+H 替换也容易出错,如果使用下面这种方式,是不是感觉更清爽一点?

public class Student : PropertyNotifyObject
{
public string Name
{
get { return this.GetValue(cu => cu.Name); }
set { this.SetValue(cu => cu.Name, value); }
}
}

2. 二次封装INotifyPropertyChanged

封装的基类PropertyNotifyObject出处我忘了,15年网上找的,源码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Threading; namespace MVVMTest
{ #region 封装WPF属性 public class MyCommMetoh
{
//得到属性的名称
public static string GetPropertyName<T, U>(Expression<Func<T, U>> exp)
{
string _pName = "";
if (exp.Body is MemberExpression)
{
_pName = (exp.Body as MemberExpression).Member.Name;
}
else if (exp.Body is UnaryExpression)
{
_pName = ((exp.Body as UnaryExpression).Operand as MemberExpression).Member.Name;
}
return _pName;
}
} public class NotifyPropertyBase : INotifyPropertyChanged
{
/// <summary>
/// Returns a dispatcher for multi-threaded scenarios
/// </summary>
/// <returns></returns>
public static Dispatcher GetDispatcher(DispatcherObject source = null)
{
//use the application's dispatcher by default
if (Application.Current != null) return Application.Current.Dispatcher; //fallback for WinForms environments
if (source != null && source.Dispatcher != null) return source.Dispatcher; //ultimatively use the thread's dispatcher
return Dispatcher.CurrentDispatcher;
} #region INotifyPropertyChanged
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
GetDispatcher().BeginInvoke((Action)delegate
{
try
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
catch (Exception ex)
{
var msg = string.Format("发送UI属性变化事件异常,属性名称: {0}, 异常详细信息: {1}", propertyName, ex.ToString());
}
}
); }
}
public void OnPropertyChanged<T>(Expression<Func<T>> expression)
{
if (PropertyChanged != null)
{
var propertyName = ((MemberExpression)expression.Body).Member.Name;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public static class NotifyPropertyBaseEx
{
public static void OnPropertyChanged<T, U>(this T npb, Expression<Func<T, U>> exp) where T : NotifyPropertyBase, new()
{
string _PropertyName = MyCommMetoh.GetPropertyName(exp);
npb.OnPropertyChanged(_PropertyName);
}
} public class PropertyNotifyObject : NotifyPropertyBase, IDisposable
{
public PropertyNotifyObject()
{
} Dictionary<object, object> _ValueDictionary = new Dictionary<object, object>(); #region 根据属性名得到属性值
public T GetPropertyValue<T>(string propertyName)
{
if (string.IsNullOrEmpty(propertyName)) throw new ArgumentException("invalid " + propertyName);
object _propertyValue;
if (!_ValueDictionary.TryGetValue(propertyName, out _propertyValue))
{
_propertyValue = default(T);
_ValueDictionary.Add(propertyName, _propertyValue);
}
return (T)_propertyValue;
}
#endregion public void SetPropertyValue<T>(string propertyName, T value)
{
if (!_ValueDictionary.ContainsKey(propertyName) || _ValueDictionary[propertyName] != (object)value)
{
_ValueDictionary[propertyName] = value;
OnPropertyChanged(propertyName);
}
} #region Dispose
public void Dispose()
{
DoDispose();
}
~PropertyNotifyObject()
{
DoDispose();
}
void DoDispose()
{
if (_ValueDictionary != null)
_ValueDictionary.Clear();
}
#endregion
}
public static class PropertyNotifyObjectEx
{
public static U GetValue<T, U>(this T t, Expression<Func<T, U>> exp) where T : PropertyNotifyObject
{
string _pN = MyCommMetoh.GetPropertyName(exp);
return t.GetPropertyValue<U>(_pN);
} public static void SetValue<T, U>(this T t, Expression<Func<T, U>> exp, U value) where T : PropertyNotifyObject
{
string _pN = MyCommMetoh.GetPropertyName(exp);
t.SetPropertyValue<U>(_pN, value);
}
} #endregion
}

3. Demo展示、源码下载

源码下载:MVVMTest

展示效果:

除非注明,文章均由 Dotnet9 整理发布,欢迎转载。


转载请注明本文地址:https://dotnet9.com/8572.html


欢迎扫描下方二维码关注 Dotnet9 的微信公众号,本站会及时推送最新技术文章



时间如流水,只能流去不流回!

这段时间在家,除了睡觉,也不要忘了学习。

简化MVVM属性设置和修改 - .NET CORE(C#) WPF开发的更多相关文章

  1. Prism+MaterialDesign+EntityFramework Core+Postgresql WPF开发总结 之 基础篇

    本着每天记录一点成长一点的原则,打算将目前完成的一个WPF项目相关的技术分享出来,供团队学习与总结. 总共分三个部分: 基础篇主要争对C#初学者,巩固C#常用知识点: 中级篇主要争对WPF布局与美化, ...

  2. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发 阅读导航 本文背景 代码 ...

  3. Prism+MaterialDesign+EntityFramework Core+Postgresql WPF开发总结 之 中级篇

    本着每天记录一点成长一点的原则,打算将目前完成的一个WPF项目相关的技术分享出来,供团队学习与总结. 总共分三个部分: 基础篇主要争对C#初学者,巩固C#常用知识点: 中级篇主要争对WPF布局与Mat ...

  4. 少量代码设计一个登录界面 - .NET CORE(C#) WPF开发

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 少量代码设计一个登录界面 - .NET CORE(C#) WPF开发 阅读导航 本文背景 代码 ...

  5. .net core 和 WPF 开发升讯威在线客服与营销系统:背景和产品介绍

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf-m.shengxunwei.com ...

  6. .net core 和 WPF 开发升讯威在线客服与营销系统:使用 WebSocket 实现访客端通信

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...

  7. .net core 和 WPF 开发升讯威在线客服与营销系统:使用 TCP协议 实现稳定的客服端

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...

  8. .net core 和 WPF 开发升讯威在线客服系统:怎样实现拔网线也不丢消息的高可靠通信(附视频)

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...

  9. .net core 和 WPF 开发升讯威在线客服与营销系统:系统总体架构

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...

随机推荐

  1. Python里使用转义字符\r时遇到的问题

    在Pycharm里使用转义字符\r和在IDLE里使用\r产生的结果是不一样的. 例子如下: print("你好!\r我是Python!") 输出结果为: 我是Python! 前面的 ...

  2. CSS-05-伪类及伪元素选择器

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. iOS开发tip-图片方向

    概述 相信稍微接触过iOS图片相关操作的同学都遇到过图片旋转的问题,另外使用AVFoundation进行拍照的话就会遇到前后摄像头切换mirror问题就让人更摸不着头脑了.今天就简单和大家聊一下iOS ...

  4. zookeeper3.4.6安装

    1.关闭防火墙 service iptables stop chkconfig iptables off 2.编辑hosts文件: vi /etc/hosts 192.168.99.6 JacK6 1 ...

  5. 机器学习李航——Adaboost课本例题实现

    例8.1Adaboost的例子 注意求D3或者D4的时候只需要把w替换一下就行,记得还得改阈值.这个代码算个半自动的,因为还需要手动改一下. import numpy as np def getA(e ...

  6. 虚拟机ubuntu系统怎么添加 VMware tools

    首先弹出光盘 然后安装 点击安装VMware tools 然后进入光盘 打开VMware tools 文件夹 将解压文件拉到桌面上 打开桌面上的文件夹 不选中文件 然后键入下面的内容 输入密码 输入y ...

  7. 虚拟机 ubuntu系统忘记密码如何进入

    重启 虚拟机 按住shift键 会出现下面的界面 按住‘e’进入下面的界面往下翻 更改红框勾到的字符串为:  rw init=/bin/bash 然后按F10进行引导 然后输入 :”passwd”  ...

  8. 实验16:ACL

    实验13-1:标准ACL Ø    实验目的通过本实验可以掌握:(1)ACL 设计原则和工作过程(2)定义标准ACL(3)应用ACL(4)标准ACL 调试 Ø    拓扑结构 本实验拒绝PC1 所在网 ...

  9. VirtualBox 安装ghost版windows XP

    昨天尝试在VirtualBox中安装深度技术的GhostXP SP3 V8.02版本的系统,可是安装过程中出现了问题,无法安装,错误提示如下图: 昨天搞了一会,没有结果,今天对于这个无法安装的问题耿耿 ...

  10. High一下!

    代码: <a title="把这个链接拖到你的Chrome收藏夹工具栏中" href='javascript:(function() { function c() { var ...