概述

在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Part01 中,我们针对 DataGrid 控件的 CollectionView 部分做了详细分享。而在本篇,我们会对 Utilities 文件夹中的类做详细的分享。

下面是 Windows Community Toolkit Sample App 的示例截图和 code/doc 地址:

Windows Community Toolkit Doc - DataGrid

Windows Community Toolkit Source Code - DataGrid

Namespace: Microsoft.Toolkit.Uwp.UI.Controls; Nuget: Microsoft.Toolkit.Uwp.UI.Controls.DataGrid;

开发过程

首先再来看一下 Utilities 文件夹的代码结构:

可以看到 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)
{
    ;
    ;

    foreach (Range<T> range in _list)
    {
         && range.UpperBound >= startIndex)
        {
            if (startIndex < range.LowerBound)
            {
                return false;
            }

            start = startIndex;
            end = range.UpperBound;
            if (end >= endIndex)
            {
                return true;
            }
        }
        )
        {
            )
            {
                return false;
            }

            end = range.UpperBound;
            if (end >= endIndex)
            {
                return true;
            }
        }
    }

    return false;
}

2) FindRangeIndex()

获取当前 range 的索引,计算过程是:遍历 list 中的 range 集合,计算 front 和 end,每次获取中间值,如果 range 的上下限包含该 index,返回该 index;否则,根据 front 和 end 的对比,计算值作为索引返回;

private int FindRangeIndex(int index)
{
    )
    {
        ;
    }

    ;
    ;
    Range<T> range = null;
    while (end > front)
    {
        ;
        range = _list[median];
        if (range.UpperBound < index)
        {
            front = median + ;
        }
        else if (range.LowerBound > index)
        {
            end = median - ;
        }
        else
        {
            return median;
        }
    }

    if (front == end)
    {
        range = _list[front];
        if (range.ContainsIndex(index) || (range.UpperBound < index))
        {
            return front;
        }
        else
        {
            ;
        }
    }
    else
    {
        return end;
    }
}

4. TypeHelper

该类是 DataGrid 控件的类型帮助类,主要功能是获取类型,属性信息,显示名等信息。这些方法都是较通用的方法,大家如果有兴趣,可以去详细查看代码,这里不做赘述。

5. ValidationUtil

该类的主要功能是 dataGrid 控件的校验,我们来看看 FindEqualValidationResult 方法:

public static ValidationResult FindEqualValidationResult(this ICollection<ValidationResult> collection, ValidationResult target)
{
    foreach (ValidationResult oldValidationResult in collection)
    {
        if (oldValidationResult.ErrorMessage == target.ErrorMessage)
        {
            bool movedOld = true;
            bool movedTarget = true;
            IEnumerator<string> oldEnumerator = oldValidationResult.MemberNames.GetEnumerator();
            IEnumerator<string> targetEnumerator = target.MemberNames.GetEnumerator();
            while (movedOld && movedTarget)
            {
                movedOld = oldEnumerator.MoveNext();
                movedTarget = targetEnumerator.MoveNext();

                if (!movedOld && !movedTarget)
                {
                    return oldValidationResult;
                }

                if (movedOld != movedTarget || oldEnumerator.Current != targetEnumerator.Current)
                {
                    break;
                }
            }
        }
    }

    return null;
}

6. VisualStates

该类是 DataGrid 控件的可视化状态类,主要分类一下几种状态组:

  • Common - Normal,PointerOver,Pressed,Disabled
  • Expanded - Expanded,Collapsed,Empty
  • Focus - Unfocused,Focused
  • Selection - Selected,Unselected
  • Active - Active,Inactive
  • Current - Regular,Current,CurrentWithFocus
  • Interaction - Display,Editing
  • Sort - Unsorted,SortAscending,SortDescending
  • Validation - Invalid,RowInvalid,RowValid,Valid
  • ScrollBarsSeparator - SeparatorExpanded,SeparatorCollapsed,SeparatorExpandedWithoutAnimation,SeparatorCollapsedWithoutAnimation
  • ScrollBars - TouchIndicator,MouseIndicator,MouseIndicatorFull,NoIndicator

总结

这里我们把 DataGrid 的 Utilities 相关类介绍完成了,作为 DataGrid 相关分享的第二篇,后面我们会继续分享最重要的 DataGrid 的相关重点。

最后,再跟大家安利一下 WindowsCommunityToolkit 的官方微博:https://weibo.com/u/6506046490大家可以通过微博关注最新动态。

衷心感谢 WindowsCommunityToolkit 的作者们杰出的工作,感谢每一位贡献者,Thank you so much, ALL WindowsCommunityToolkit AUTHORS !!!

Windows Community Toolkit 4.0 - DataGrid - Part02的更多相关文章

  1. Windows Community Toolkit 4.0 - DataGrid - Part03

    概述 在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Part02 中,我们针对 DataGrid 控件的 Utilities 部分做了详细分享.而在 ...

  2. Windows Community Toolkit 4.0 - DataGrid - Part01

    概述 在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Overview 中,我们对 DataGrid 控件做了一个概览的介绍,今天开始我们会做进一步的 ...

  3. Windows Community Toolkit 4.0 - DataGrid - Overview

    概述 Windows Community Toolkit 4.0 于 2018 月 8 月初发布:Windows Community Toolkit 4.0 Release Note. 4.0 版本相 ...

  4. Windows Community Toolkit 3.0 - UniformGrid

    概述 UniformGrid 控件是一个响应式的布局控件,允许把 items 排列在一组均匀分布的行或列中,以填充整体的可用显示空间,形成均匀的多个网格.默认情况下,网格中的每个单元格大小相同. 这是 ...

  5. Windows Community Toolkit 3.0 - InfiniteCanvas

    概述 InfiniteCanvas 是一个 Canvas 控件,它支持无限画布的滚动,支持 Ink,文本,格式文本,画布缩放操作,撤销重做操作,导入和导出数据. 这是一个非常实用的控件,在“来画视频” ...

  6. Windows Community Toolkit 3.0 - Gaze Interaction

    概述 Gaze Input & Tracking - 也就是视觉输入和跟踪,是一种和鼠标/触摸屏输入非常不一样的交互方式,利用人类眼球的识别和眼球方向角度的跟踪,来判断人眼的目标和意图,从而非 ...

  7. Windows Community Toolkit 3.0 - CameraPreview

    概述 Windows Community Toolkit 3.0 于 2018 年 6 月 2 日 Release,同时正式更名为 Windows Community Toolkit,原名为 UWP ...

  8. Windows Community Toolkit 3.0 新功能 在WinForms 和 WPF 使用 UWP 控件

    本文告诉大家一个令人震惊的消息,Windows Community Toolkit 有一个大更新,现在的版本是 3.0 .最大的提升就是 WinForm 和 WPF 程序可以使用部分 UWP 控件. ...

  9. 与众不同 windows phone (44) - 8.0 位置和地图

    [源码下载] 与众不同 windows phone (44) - 8.0 位置和地图 作者:webabcd 介绍与众不同 windows phone 8.0 之 位置和地图 位置(GPS) - Loc ...

随机推荐

  1. Tmux 日常快捷键 及配置

    使用Linux的人不管是开发人员.还是运维人员都不可避免的需要使用到终端模拟器(比如,gnome-terminal)去执行一些命令或者脚本. tmux可以将终端模拟器方便的切分为不同的小窗口如下图如示 ...

  2. ctypes库调用dll的个人见解

    最近着手开发一个小东西涉及到了API接口的知识点, 第一次使用到了ctypes库,在网上找了一大圈,基本都是讲add.dll之后就没了. 就像下面这个: from ctypes import * dl ...

  3. .net、mono和C#

    .net wiki:en  chs .net版本 公共语言运行时(CLR) 发布时间 随同分发的Visual Studio 预装于windows系统 支持的windows系统 1.0 1.0 2002 ...

  4. RHEL 5.7 使用rpm安装XtraBackup问题总结

    在Red Hat Enterprise Linux Server release 5.7 (Tikanga)上使用RPM方式安装Percona Xtrabackup 2.4.6时遇到了一些问题,特意总 ...

  5. ros中自定义消息 报错 ImportError: No module named em

    大家好,欢迎来到我的博客,之前写的都是比较松散的,鉴于工作的原因,之后的随笔将持续更新ROS以及linux使用方面的随笔,欢迎大家留言,相互学习 ——————————————————————————— ...

  6. SQL Server datetime类型转换超出范围的报错

    一个很基础的插入语句: insert into table1 select col1,convert(datetime,col2),convert(datetime,col3),col4,col5 f ...

  7. iOS application/json上传文件等

    在和sever后台交互的过程中.有时候.他们需要我们iOS开发者以“application/json”形式上传. NSString *accessUrl = [NSString stringWithF ...

  8. shell read变量的读入

    shell变量的输入: shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入获取,read为bash内置命令,可以通过help read查看帮助. 语法格式: read [参数 ...

  9. #007 C语言大作业学生管理系统第四天

    第四天还差恢复已删除学生功能 对于我来说,已经开始很复杂了. 小细节太重要了,边写边出错 1 #include<stdio.h> #include<stdlib.h> #inc ...

  10. 【Teradata】数据库初始化(sysinit和dip工具)

    1.删除数据库对象 (1)使用root用户登录数据库节点 arcmain .LOGON 127.0.0.1/dbc,dbc_password; (2)清理所有数据库对象及数据 DELETE DATAB ...