搜索
首页后端开发C++如何将 AddRange 方法和 INotifyCollectionChanging 添加到 .NET 中的 ObservableCollection?

How to Add an AddRange Method and INotifyCollectionChanging to an ObservableCollection in .NET?

.NET中的ObservableCollection类提供了一个动态数据集合,可以在添加、删除或重新排列项时发出通知。但是,它缺少AddRange方法,该方法无法一次批量添加多个项。此外,INotifyCollectionChanging接口可用于在更改提交到集合之前跟踪更改,从而提供取消更改的机会。

以下是如何在ObservableCollection中实现AddRange方法并集成INotifyCollectionChanging

C#:

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Specialized;

public class ObservableRangeCollection<T> : ObservableCollection<T>, INotifyCollectionChanging<T>
{
    public event NotifyCollectionChangingEventHandler<T> CollectionChanging;

    protected override void OnCollectionChanging(NotifyCollectionChangingEventArgs e)
    {
        // 创建一个包含更改信息的事件参数
        var typedEventArgs = new NotifyCollectionChangingEventArgs<T>(e.Action, e.NewItems != null ? (IEnumerable<T>)e.NewItems : null, e.OldItems != null ? (IEnumerable<T>)e.OldItems : null, e.Index);

        // 触发自定义事件,允许取消操作
        CollectionChanging?.Invoke(this, typedEventArgs);

        // 检查是否取消
        if (typedEventArgs.Cancel)
        {
            return;
        }

        // 调用基类的OnCollectionChanging方法
        base.OnCollectionChanging(e);
    }

    public void AddRange(IEnumerable<T> collection)
    {
        if (collection == null)
        {
            throw new ArgumentNullException(nameof(collection));
        }

        // 触发CollectionChanging事件
        OnCollectionChanging(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection));

        // 添加项目
        foreach (T item in collection)
        {
            Add(item);
        }
    }
}

//自定义INotifyCollectionChanging接口,用于类型安全
public interface INotifyCollectionChanging<T> : INotifyCollectionChanged
{
    event NotifyCollectionChangingEventHandler<T> CollectionChanging;
}

//自定义事件处理程序,用于类型安全
public delegate void NotifyCollectionChangingEventHandler<T>(object sender, NotifyCollectionChangingEventArgs<T> e);

//自定义事件参数,用于类型安全
public class NotifyCollectionChangingEventArgs<T> : NotifyCollectionChangedEventArgs
{
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, IEnumerable<T> newItems) : base(action, newItems) { }
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, IEnumerable<T> newItems, IEnumerable<T> oldItems) : base(action, newItems, oldItems) { }
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, IEnumerable<T> newItems, int index) : base(action, newItems, index) { }
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, T newItem, int index) : base(action, newItem, index) { }
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, T oldItem, int index) : base(action, null, oldItem, index) { }
    public bool Cancel { get; set; }
}

VB.NET:

Imports System
Imports System.Collections.ObjectModel
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.ComponentModel

Public Class ObservableRangeCollection(Of T)
    Inherits ObservableCollection(Of T)
    Implements INotifyCollectionChanging(Of T)

    Public Event CollectionChanging As NotifyCollectionChangingEventHandler(Of T) Implements INotifyCollectionChanging(Of T).CollectionChanging

    Protected Overrides Sub OnCollectionChanging(e As NotifyCollectionChangedEventArgs)
        Dim typedEventArgs As New NotifyCollectionChangingEventArgs(Of T)(e.Action, If(e.NewItems IsNot Nothing, DirectCast(e.NewItems, IEnumerable(Of T)), Nothing), If(e.OldItems IsNot Nothing, DirectCast(e.OldItems, IEnumerable(Of T)), Nothing), e.Index)
        RaiseEvent CollectionChanging(Me, typedEventArgs)
        If typedEventArgs.Cancel Then
            Return
        End If
        MyBase.OnCollectionChanging(e)
    End Sub

    Public Sub AddRange(collection As IEnumerable(Of T))
        If collection Is Nothing Then
            Throw New ArgumentNullException("collection")
        End If

        OnCollectionChanging(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection))

        For Each item As T In collection
            Add(item)
        Next
    End Sub
End Class

'自定义INotifyCollectionChanging接口,用于类型安全
Public Interface INotifyCollectionChanging(Of T)
    Inherits INotifyCollectionChanged
    Event CollectionChanging As NotifyCollectionChangingEventHandler(Of T)
End Interface

'自定义事件处理程序,用于类型安全
Public Delegate Sub NotifyCollectionChangingEventHandler(Of T)(sender As Object, e As NotifyCollectionChangingEventArgs(Of T))

'自定义事件参数,用于类型安全
Public Class NotifyCollectionChangingEventArgs(Of T)
    Inherits NotifyCollectionChangedEventArgs
    Public Sub New(action As NotifyCollectionChangedAction, newItems As IEnumerable(Of T))
        MyBase.New(action, newItems)
    End Sub
    Public Sub New(action As NotifyCollectionChangedAction, newItems As IEnumerable(Of T), oldItems As IEnumerable(Of T))
        MyBase.New(action, newItems, oldItems)
    End Sub
    Public Sub New(action As NotifyCollectionChangedAction, newItems As IEnumerable(Of T), index As Integer)
        MyBase.New(action, newItems, index)
    End Sub
    Public Sub New(action As NotifyCollectionChangedAction, newItem As T, index As Integer)
        MyBase.New(action, newItem, index)
    End Sub
    Public Sub New(action As NotifyCollectionChangedAction, oldItem As T, index As Integer)
        MyBase.New(action, Nothing, oldItem, index)
    End Sub
    Public Property Cancel As Boolean
End Class

通过这种方法,您可以在能够跟踪并在发生之前取消更改的同时,从批量操作中受益。 代码已改进,使其更健壮,并处理了 INotifyCollectionChanging 事件的类型安全问题。 它还包含了对 OnCollectionChanging 方法的重写,以确保在触发自定义事件后,仍然调用基类的 OnCollectionChanging 方法,从而保证 ObservableCollection 的正常功能。

以上是如何将 AddRange 方法和 INotifyCollectionChanging 添加到 .NET 中的 ObservableCollection?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
c语言函数返回值的类型有哪些?返回值是由什么决定的?c语言函数返回值的类型有哪些?返回值是由什么决定的?Mar 03, 2025 pm 05:52 PM

本文详细介绍了C函数返回类型,包括基本(int,float,char等),派生(数组,指针,结构)和void类型。 编译器通过函数声明和返回语句确定返回类型,执行

Gulc:从头开始建造的C库Gulc:从头开始建造的C库Mar 03, 2025 pm 05:46 PM

Gulc是一个高性能的C库,优先考虑最小开销,积极的内衬和编译器优化。 其设计非常适合高频交易和嵌入式系统等关键应用程序,其设计强调简单性,模型

c语言函数的定义和调用规则是什么c语言函数的定义和调用规则是什么Mar 03, 2025 pm 05:53 PM

本文解释了C函数声明与定义,参数传递(按值和指针),返回值以及常见的陷阱,例如内存泄漏和类型不匹配。 它强调了声明对模块化和省份的重要性

c语言函数格式字母大小写转换步骤c语言函数格式字母大小写转换步骤Mar 03, 2025 pm 05:53 PM

本文详细介绍了字符串案例转换的C功能。 它可以通过ctype.h的toupper()和tolower()解释,并通过字符串迭代并处理零终端。 常见的陷阱,例如忘记ctype.h和修改字符串文字是

c语言函数返回值在内存保存在哪里?c语言函数返回值在内存保存在哪里?Mar 03, 2025 pm 05:51 PM

本文研究C函数返回值存储。 较小的返回值通常存储在寄存器中以备速度;较大的值可能会使用指针来记忆(堆栈或堆),影响寿命并需要手动内存管理。直接ACC

distinct用法和短语分享distinct用法和短语分享Mar 03, 2025 pm 05:51 PM

本文分析了形容词“独特”的多方面用途,探索其语法功能,常见的短语(例如,“不同于”,“完全不同”),以及在正式与非正式中的细微应用

C标准模板库(STL)如何工作?C标准模板库(STL)如何工作?Mar 12, 2025 pm 04:50 PM

本文解释了C标准模板库(STL),重点关注其核心组件:容器,迭代器,算法和函子。 它详细介绍了这些如何交互以启用通用编程,提高代码效率和可读性t

如何有效地使用STL(排序,查找,转换等)的算法?如何有效地使用STL(排序,查找,转换等)的算法?Mar 12, 2025 pm 04:52 PM

本文详细介绍了c中有效的STL算法用法。 它强调了数据结构选择(向量与列表),算法复杂性分析(例如,std :: sort vs. std vs. std :: partial_sort),迭代器用法和并行执行。 常见的陷阱

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境