C# 如何读取列表中添加的记录加载项事件
我有以下代码的列表/事件C# 如何读取列表中添加的记录加载项事件,c#,events,event-handling,C#,Events,Event Handling,我有以下代码的列表/事件 class MyList<T> : List<T> { public event EventHandler OnAdd; public void Add(T item) { if (null != OnAdd) { OnAdd(this, null); }
class MyList<T> : List<T>
{
public event EventHandler OnAdd;
public void Add(T item)
{
if (null != OnAdd)
{
OnAdd(this, null);
}
base.Add(item);
}
}
MyList<connectedUser> myListUsers = new MyList<connectedUser>();
myListUsers.OnAdd += new EventHandler (myListUsers_OnAdd );
private void myListUsers_OnAdd(object sender, EventArgs e)
{
}
public class connectedUser
{
public string userID { get; set; }
public string marketID { get; set; }
}
类MyList:列表
{
公共事件处理程序OnAdd;
公共作废新增(T项)
{
if(null!=OnAdd)
{
OnAdd(this,null);
}
基础。添加(项目);
}
}
MyList myListUsers=新建MyList();
myListUsers.OnAdd+=新事件处理程序(myListUsers\u OnAdd);
私有void myListUsers_OnAdd(对象发送方,事件参数e)
{
}
公共类连接器
{
公共字符串用户标识{get;set;}
公共字符串ID{get;set;}
}
在我的on-OnAdd事件(myListUsers\u-OnAdd)中,我希望我插入的对象作为参数,换句话说,我如何读取事件中插入的对象 示例:
var collection = new ObservableCollection<int>();
collection.CollectionChanged += CollectionOnCollectionChanged;
var collection=newobservetecollection();
collection.CollectionChanged+=CollectionOnCollectionChanged;
您的方法如下所示:
private static void CollectionOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) {
//notifyCollectionChangedEventArgs.NewItems <= this are the items that got added
//notifyCollectionChangedEventArgs.OldItems <= this are the items that got removed
}
私有静态无效CollectionOnCollectionChanged(对象发送方,NotifyCollectionChangedEventArgs NotifyCollectionChangedEventArgs){
//notifyCollectionChangedEventArgs.NewItems我添加了一个示例。您在哪里添加了示例?好的,我知道了。对不起,您可以循环查看这些项目,如果没有添加或删除任何项目,列表应该为空。