如何与在列表中更改事件列表 [英] How to have Lists with events upon change in List

查看:97
本文介绍了如何与在列表中更改事件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在事件新手。 这个例子显示,一件事被调用每时间ArrayList中被改变。我想知道如何使用泛型做到这一点。为了您实现IList或扩展列表?我试图代码,但我坚持

 使用系统; 
使用System.Collections.Generic;

命名活动
{
公众委托无效ChangedEventHandler(对象发件人,EventArgs e)条;

公共类ListWithChangedEvent< T> :IList的< T>
{
公共事件ChangedEventHandler更改;

受保护的虚拟无效调用onChanged(EventArgs的发送)
{
如果
{
改变(这一点,E)(改变!= NULL);
}
}

公共无效添加(T值)
{
base.Add(值);
调用onChanged(EventArgs.Empty);
}

公共无效清除()
{
base.Clear();
调用onChanged(EventArgs.Empty);
}

公共牛逼这个[INT指数]
{

{
基[指数] =值;
调用onChanged(EventArgs.Empty);
}
}
}

类EventListener的
{
私人ListWithChangedEvent<串GT;清单;

公共事件监听(ListWithChangedEvent<串GT;清单)
{
=列出清单;
List.Changed + =新ChangedEventHandler(引发ListChanged);
}

私人无效的ListChanged(对象发件人,EventArgs五)
{
Console.WriteLine(这就是所谓的事件触发时。);
}

公共无效分离()
{
List.Changed - =新ChangedEventHandler(引发ListChanged);
名单= NULL;
}
}

类节目
{
公共静态无效的主要(字串[] args)
{
ListWithChangedEvent<字符串>名单=新ListWithChangedEvent<串GT;();

事件监听监听器=新的EventListener(名单);

list.Add(项目1);
list.Clear();
listener.Detach();
}
}
}


解决方案

您可以使用的ObservableCollection,你可以扩展它!!结果
命名空间:System.Collections.ObjectModel结果
集:WindowsBase(在WindowsBase.dll)



这集触发每当列表更改的事件。(说的都是添加任何物品,从列表中删除)
结果



但请注意:的,如果对象的内部属性是抱着变化的ObservableCollection犯规火灾事件。如果你需要那些让我知道,我已经扩展了可观察到的集合,具有功能也。


I'm a newbie in events. This example shows that an event is invoked every time the ArrayList is changed. I would like to know how to do it using generics. To you implement IList or extend List? I tried to code it but I'm stuck.

using System;
using System.Collections.Generic;

namespace Events
{
    public delegate void ChangedEventHandler(object sender, EventArgs e);

    public class ListWithChangedEvent<T> : IList<T>
    {
        public event ChangedEventHandler Changed;

        protected virtual void OnChanged(EventArgs e)
        {
            if (Changed != null)
            {
                Changed(this, e);
            }
        }

        public void Add(T value)
        {
            base.Add(value);
            OnChanged(EventArgs.Empty);
        }

        public void Clear()
        {
            base.Clear();
            OnChanged(EventArgs.Empty);
        }

        public T this[int index]
        {
            set
            {
                base[index] = value;
                OnChanged(EventArgs.Empty);
            }
        }
    }

    class EventListener
    {
        private ListWithChangedEvent<string> List;

        public EventListener(ListWithChangedEvent<string> list)
        {
            List = list;
            List.Changed += new ChangedEventHandler(ListChanged);
        }

        private void ListChanged(object sender, EventArgs e)
        {
            Console.WriteLine("This is called when the event fires.");
        }

        public void Detach()
        {
            List.Changed -= new ChangedEventHandler(ListChanged);
            List = null;
        }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            ListWithChangedEvent<string> list = new ListWithChangedEvent<string>();

            EventListener listener = new EventListener(list);

            list.Add("item 1");
            list.Clear();
            listener.Detach();
        }
    }
}

解决方案

You can use ObservableCollection and you can extend it !!
Namespace: System.Collections.ObjectModel
Assembly: WindowsBase (in WindowsBase.dll)

This collection fires events whenever the list is changed.(say any items are added, removed from list)

But note : The ObservableCollection doesnt fire events if the internal properties of objects it is holding changes. If you need that do let me know, I have extended the Observable collection to have that feature also.

这篇关于如何与在列表中更改事件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆