这是一个 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 属性的获取,是采用此方法获取的

    /// <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 方法的代码如下

    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 等类型


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/WPF-%E5%85%B3%E4%BA%8E%E5%B0%86-ManipulationDeltaEventArgs-%E7%9A%84-Manipulators-%E5%B1%9E%E6%80%A7%E8%BF%94%E5%9B%9E%E5%80%BC%E4%BF%AE%E6%94%B9%E4%B8%BA-ReadOnlyCollection-%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%8F%90%E8%AE%AE.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利