概述
可以看到 Utilities 文件夹中的类主要是一些基础和帮助类,下面我们来看一些重要的类代码:
1. DoubleUtil
该类的功能主要是判断两个 double 类型的值之间是否接近,大小关系等;这些方法中用到了一个 AreClose(v1, v2) 的方法,这个方法主要判断两个数值是否相近,计算方法是,当两个值的差,除以两个值的绝对值和加10.0 的值小于 double epsilon 时,认为两个数值是接近的。而 double epsilon 表示大于零的最小 double 数值。
internal const double DBL_EPSILON = 1.1102230246251567e-016;
public static bool AreClose(double value1, double value2) { // in case they are Infinities (then epsilon check does not work) if (value1 == value2) { return true; } // This computes (|value1-value2| / (|value1| + |value2| + 10.0)) < DBL_EPSILON double eps = (Math.Abs(value1) + Math.Abs(value2) + 10.0) * DBL_EPSILON; double delta = value1 - value2; return -eps < delta && eps > delta; }
2. Extensions
该类的功能是 DataGrid 控件的扩展,主要有以下扩展方法:
- IsHandlerSuspended - 处理器暂停的标识;
- ContainsChild - 遍历可视化树,判断当前控件是否包含某个 child 元素,该方法在 WPF UWP 的很多控件中都有过体现;
- ContainsFocusedElement - 遍历可视化树,判断当前控件是否包含获得焦点的元素;
- GetIsReadOnly - 获取控件的只读属性;
- GetItemType - 获取元素类型,分为枚举和集合两种分类来判断;
- SetStyleWithType - 设置元素的样式;
- SetValueNoCallback - 设置值并中断回调;
- Translate - 计算起始和终止元素间的坐标移动;
- EnsureMeasured - 在控件被置于背景层时,需要计算尺寸;
- SuspendHandler - 暂停处理器的处理;
3. IndexToValueTable
该类的功能是 DataGrid 控件的索引和值表之间的处理,我们看几个重要的方法:
1) ContainsAll()
该方法的作用是判断给定的 startIndex 和 endIndex 间的索引范围,是否全部包含在表中;判断过程主要是根据 startIndex 和 endIndex 的值,以及 list 中的每个 range 的 UpperBound 和 LowerBound 的值,判断 startIndex 和 endIndex 是否包含在某个 range 中;
public bool ContainsAll(int startIndex, int endIndex) { int start = -1; int end = -1; foreach (Range<T> range in _list) { if (start == -1 && range.UpperBound >= startIndex) { if (startIndex < range.LowerBound) { return false; } start = startIndex; end = range.UpperBound; if (end >= endIndex) { return true; } } else if (start != -1) { if (range.LowerBound > end +

