title author date CreateTime categories
WPF UncommonField 类型是什么
lindexi
2018-3-8 16:25:2 +0800
2018-3-8 16:9:20 +0800
WPF .net framework .net源代码 源代码分析

本文告诉大家一个黑科技,这个黑科技在.net 框架外无法使用,这就是 UncommonField 。下面将会告诉大家这个类有什么用。

如果大家有反编译 UIElement 那么就会看到下面的代码

internal static readonly UncommonField<EventHandlersStore> EventHandlersStoreField = new UncommonField<EventHandlersStore>();

那么这个UncommonField是什么?这个类是解决DependencyObject 使用很多内存。使用这个类可以作为轻量的DependencyObject 因为他使用很少的内存。

因为使用了DependencyObject 就会创建很多默认的值,无论使用的是TextBox.Text的依赖属性还是Grid.Row附加的,都会有很多不需要使用的值。但是在框架,需要使用很少的内存,所以就使用UncommonField

如果使用UncommonField就会去掉很多元数据、校验、通知,UncommonField会使用和DependencyObject 相同的机制,让他可以存放在DependencyObject 中和其他存放的属性一样,在没有改变值的时候会使用上一级、默认的值。所以可以减少一些内存。

因为现在很少人会写出和框架一样的那么多使用依赖属性,所以就不需要使用这个功能。

下面就是UncommonField代码,我添加一些注释

     //这个类可以减少内存使用,比使用 DependencyObject 少的内存,这个类在框架使用,不在外面使用
[FriendAccessAllowed] // Built into Base, used by Core and Framework
internal class UncommonField<T>
{
/// <summary>
/// Create a new UncommonField.
/// </summary>
public UncommonField() : this(default(T))
{
} /// <summary>
/// Create a new UncommonField.
/// </summary>
/// <param name="defaultValue">The default value of the field.</param>
public UncommonField(T defaultValue)
{
_defaultValue = defaultValue;
_hasBeenSet = false; lock (DependencyProperty.Synchronized)
{
//注册方法和依赖属性相同
_globalIndex = DependencyProperty.GetUniqueGlobalIndex(null, null); DependencyProperty.RegisteredPropertyList.Add();
}
} /// <summary>
/// 从下面代码可以看到,设置值代码和依赖属性相同
/// Write the given value onto a DependencyObject instance.
/// </summary>
/// <param name="instance">The DependencyObject on which to set the value.</param>
/// <param name="value">The value to set.</param>
public void SetValue(DependencyObject instance, T value)
{
//如果传入的值是空,会有异常
if (instance != null)
{
EntryIndex entryIndex = instance.LookupEntry(_globalIndex); //设置的值如果和默认的相同,那么就直接跳过
// Set the value if it's not the default, otherwise remove the value.
if (!object.ReferenceEquals(value, _defaultValue))
{
//下面的代码进行设置值
instance.SetEffectiveValue(entryIndex, null /* dp */, _globalIndex, null /* metadata */, value, BaseValueSourceInternal.Local);
_hasBeenSet = true;
}
else
{
instance.UnsetEffectiveValue(entryIndex, null /* dp */, null /* metadata */);
}
}
else
{
throw new ArgumentNullException("instance");
}
} /// <summary>
/// 如果没有设置值,就从默认获取,或者上一级,方法和依赖属性相同
/// Read the value of this field on a DependencyObject instance.
/// </summary>
/// <param name="instance">The DependencyObject from which to get the value.</param>
/// <returns></returns>
public T GetValue(DependencyObject instance)
{
if (instance != null)
{
if (_hasBeenSet)
{
EntryIndex entryIndex = instance.LookupEntry(_globalIndex); if (entryIndex.Found)
{
object value = instance.EffectiveValues[entryIndex.Index].LocalValue; if (value != DependencyProperty.UnsetValue)
{
return (T)value;
}
}
return _defaultValue;
}
else
{
return _defaultValue;
}
}
else
{
throw new ArgumentNullException("instance");
}
} /// <summary>
/// Clear this field from the given DependencyObject instance.
/// </summary>
/// <param name="instance"></param>
public void ClearValue(DependencyObject instance)
{
if (instance != null)
{
EntryIndex entryIndex = instance.LookupEntry(_globalIndex); instance.UnsetEffectiveValue(entryIndex, null /* dp */, null /* metadata */);
}
else
{
throw new ArgumentNullException("instance");
}
} internal int GlobalIndex
{
get
{
return _globalIndex;
}
} #region Private Fields private T _defaultValue;
private int _globalIndex;
private bool _hasBeenSet; #endregion
}

从上面的代码可以自己定义一个和他一样的类,用来存放比较少的属性,但是使用不多,因为现在的软件很少需要减少那么少的内存。

参见:https://stackoverflow.com/a/18280136/6116637

https://referencesource.microsoft.com/#WindowsBase/Base/System/Windows/UncommonField.cs

2018-3-8-WPF-UncommonField-类型是什么的更多相关文章

  1. dotnet 从入门到放弃的 500 篇文章合集

    本文是记录我从入门到放弃写的博客 博客包括 C#.WPF.UWP.dotnet core .git 和 VisualStudio 和一些算法,所有博客使用 docx 保存 下载:dotnet 从入门到 ...

  2. 2018-8-10-dotnet-从入门到放弃的-500-篇文章合集

    title author date CreateTime categories dotnet 从入门到放弃的 500 篇文章合集 lindexi 2018-08-10 19:16:52 +0800 2 ...

  3. 在编写wpf界面时候中出现如下错误: 类型引用不明确。至少有两个名称空间(“System.Windows”和“System.Windows”)中已出现名为“VisualStateManager”的类型。请考虑调整程序集 XmlnsDefinition 特性。

    wpf中类型引用不明确.至少有两个名称空间(“System.Windows”和“System.Windows”)中已出现名为“VisualState 你是不是用了WPFToolKit?如果是的,那原因 ...

  4. WPF视频教程系列笔记

    视频二:XAML基础 1.顶级元素 <Window></Window>,<Page></Page>,<Application></Ap ...

  5. 10、WPF程序集

    WPF核心程序集 PresentationCore.dll:这个程序集定义了许多构成WPF GUI层基础的类型.例如包含WPF Ink API(pc笔针输入,手写输入)的支持.几个动画基元以及几个图形 ...

  6. WPF的消息机制

    前言 谈起“消息机制”这个词,我们都会想到Windows的消息机制,系统将键盘鼠标的行为包装成一个Windows Message,然后系统主动将这些Windows Message派发给特定的窗口,实际 ...

  7. C# 在WPF中使用Exceptionless异常日志框架

    登录http://exceptionless.com/官网,注册一个账户. 创建项目 选择wpf项目类型 拷贝下箭头指的这个密钥,过后程序里用的到. 下面我们打开vs,新建一个wpf的项目 打开git ...

  8. 一步一步部署WPF浏览器应用程序

    WPF浏览器应用程序与Silverlight,Asp.net相比,同是发布到服务器,在IE中运行.WPF浏览器应用程序部署起来却相对困难. 相信很多朋友在第一次部署WPF浏览器应用程序时,都遇到&qu ...

  9. WPF的消息机制(一)- 让应用程序动起来

    原文:WPF的消息机制(一)- 让应用程序动起来 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/powertoolsteam/article/det ...

  10. 手势识别(一)--手势基本概念和ChaLearn Gesture Challenge

    以下转自: http://blog.csdn.net/qq1175421841/article/details/50312565 像点击(clicks)是GUI平台的核心,轻点(taps)是触摸平台的 ...

随机推荐

  1. react-naive工作原理

    react-naive工作原理是从react的工作原理衍生出来的 react的工作原理 在react中,virtual dom 就像一个中间层,介于开发者描述的视图与实际在页面上渲染的视图之间.为了在 ...

  2. GridSearch 最佳 estimator 设置问题

    GridSearchCV 最佳 estimator 设置问题 def train_model_Grid(estimator, param_grid, cv, X_train, X_test, y_tr ...

  3. RelationNet:学习目标间关系来增强特征以及去除NMS | CVPR 2018

    论文基于NLP的注意力机制提出了目标关系模块,通过与其它目标的比对增强当前目标的特征,而且还可以代替NMS进行端到端的重复结果去除,思想十分新颖,效果也不错   来源:晓飞的算法工程笔记 公众号 论文 ...

  4. DETR:Facebook提出基于Transformer的目标检测新范式,性能媲美Faster RCNN | ECCV 2020 Oral

    DETR基于标准的Transorfmer结构,性能能够媲美Faster RCNN,而论文整体思想十分简洁,希望能像Faster RCNN为后续的很多研究提供了大致的思路   来源:晓飞的算法工程笔记 ...

  5. KingbaseES 与 Oracle XML 语法比较

    KingbaseES 内置支持 XML 相关操作,也可以通过xml2 插件进行扩展支持.以下通过例子介绍 KingbaseES XML 与Oracle 在用法上存在的一些差异. 一.数据准备 crea ...

  6. 浅谈JVM整体架构与调优参数

    本文分享自华为云社区<[性能优化]JVM整体架构与调优参数说明>,作者: 冰 河. JVM的分类 这里,我们先来说说什么是VM吧,VM的中文含义为:虚拟机,指的是使用软件的方式模拟具有完整 ...

  7. Tomcat内存马回显

    回顾JSP马 详情见:https://www.cnblogs.com/F12-blog/p/18111253 之前说的都是利用 jsp 注入内存马,但 Web 服务器中的 jsp 编译器还是会编译生成 ...

  8. 带你玩转OpenHarmony AI-基于海思NNIE的AI能力自定义

    简介 相信大家从玩转OpenAtom OpenHarmony(简称"OpenHarmony")AI系列专题的其他文章中,已经拓展了OpenHarmony AI在智慧出行.智慧办公等 ...

  9. MogDB 使用向量化执行引擎进行调优

    MogDB 使用向量化执行引擎进行调优 本文出处:https://www.modb.pro/db/430318 MogDB 数据库支持行执行引擎和向量化执行引擎,分别对应行存表和列存表. 一次一个 b ...

  10. centos 6.4更新163源

    centos 6.4更新163源   1. 备份现在的源文件    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base. ...