前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >WPF 关于将 ManipulationDeltaEventArgs 的 Manipulators 属性返回值修改为 ReadOnlyCollection 类型的提议

WPF 关于将 ManipulationDeltaEventArgs 的 Manipulators 属性返回值修改为 ReadOnlyCollection 类型的提议

作者头像
林德熙
发布于 2022-03-28 05:53:59
发布于 2022-03-28 05:53:59
1.1K00
代码可运行
举报
文章被收录于专栏:林德熙的博客林德熙的博客
运行总次数:0
代码可运行

这是一个 WPF 框架的 API 变更提议,记录一下博客

讨论的地方是: How about change the type of ManipulationDeltaEventArgs.Manipulators property to ReadOnlyCollection · Discussion #6249 · dotnet/wpf

问题:

在 WPF 里,放在 ManipulationDeltaEventArgs 类型的 Manipulators 属性,当前的返回值是 IEnumerable<IManipulator> 类型。然而此类型的返回值用起来比较坑,例如获取元素数量,就需要用到 Linq 的 Count 方法

然而在 WPF 框架的实现,在 Manipulators 属性的获取,是采用此方法获取的

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    /// <summary>
    ///     Provides an update on an ocurring manipulation.
    /// </summary>
    public sealed class ManipulationDeltaEventArgs : InputEventArgs
    {
    	// 忽略代码

        /// <summary>
        ///     The Manipulators for this manipulation.
        /// </summary>
        public IEnumerable<IManipulator> Manipulators
        {
            get
            {
                if (_manipulators == null)
                {
                    _manipulators = ((ManipulationDevice)Device).GetManipulatorsReadOnly();
                }
                return _manipulators;
            }
        }

        private IEnumerable<IManipulator> _manipulators;
    }

更底层的 ManipulationDevice 的 GetManipulatorsReadOnly 方法的代码如下

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    internal sealed class ManipulationDevice : InputDevice
    {
        internal IEnumerable<IManipulator> GetManipulatorsReadOnly()
        {
            if (_manipulators != null)
            {
                return new ReadOnlyCollection<IManipulator>(_manipulators);
            }
            else
            {
                return new ReadOnlyCollection<IManipulator>(new List<IManipulator>(2));
            }
        }
        private List<IManipulator> _manipulators;
    }

实际上,以上代码有两个坑,一个就是 _manipulators 的初始化问题,另一个就是,为什么在 _manipulators 是空的时候,传入 new List 初始个数是 2 的值

提议:

修改 ManipulationDeltaEventArgs 的 Manipulators 属性的返回值为 ReadOnlyCollection 或者 IReadOnlyCollection 或者 IReadOnlyList 等类型

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验