Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Events Not Firing
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01443862
Message ID:
01443988
Vues:
26
>>>I have a generic collection:
>>>
>>>
>>>public class Collection<T> : IEnumerable<T>
>>>{
>>>    public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
>>>    public delegate void ListClearedEventHandler(object sender, ListClearedEventArgs e);
>>>
>>>    public event ListClearedEventHandler Cleared;
>>>    public event EventHandler<EventArgs_Generic<T>> ItemAdded;
>>>    public event EventHandler<EventArgs_Generic<T>> ItemRemoved;
>>>
>>>    List<T> _List = new List<T>();
>>>
>>>    private int _Count = 0;
>>>    public int Count
>>>    {
>>>        get { return _Count; }
>>>    }
>>>    public void Add(T item)
>>>    {
>>>        _List.Add(item);
>>>        _Count++;
>>>
>>>        if (ItemAdded != null)
>>>        {
>>>            EventArgs_Generic<T> args = new EventArgs_Generic<T>(item);
>>>            ItemAdded(this, args);
>>>        }
>>>    }
>>>    public void Clear()
>>>    {
>>>        _List.Clear();
>>>        _Count = 0;
>>>
>>>        if (Cleared != null)
>>>        {
>>>            ListClearedEventArgs args = new ListClearedEventArgs();
>>>            Cleared(this, args);
>>>        }
>>>    }
>>>    public void Insert(int index, T item)
>>>    {
>>>        _List.Insert(index, item);
>>>        _Count++;
>>>
>>>        if (ItemAdded != null)
>>>        {
>>>            EventArgs_Generic<T> args = new EventArgs_Generic<T>(item);
>>>            ItemAdded(this, args);
>>>        }
>>>    }
>>>    public void Remove(T item)
>>>    {
>>>        _List.Remove(item);
>>>        _Count--;
>>>
>>>        if (ItemRemoved != null)
>>>        {
>>>            EventArgs_Generic<T> args = new EventArgs_Generic<T>(item);
>>>            ItemRemoved(this, args);
>>>        }
>>>    }
>>>    public T GetItem(int index)
>>>    {
>>>        return _List[index];
>>>    }
>>>
>>>    IEnumerator<T> IEnumerable<T>.GetEnumerator()
>>>    {
>>>        return _List.GetEnumerator();
>>>    }
>>>    IEnumerator IEnumerable.GetEnumerator()
>>>    {
>>>        return _List.GetEnumerator();
>>>    }
>>>}
>>>
>>>public class EventArgs_Generic<T> : EventArgs
>>>{
>>>    private T _TargetObject;
>>>    public T TargetObject
>>>    {
>>>        get { return _TargetObject; }
>>>    }
>>>
>>>    public EventArgs_Generic()
>>>    {
>>>    }
>>>    public EventArgs_Generic(T Target)
>>>    {
>>>        _TargetObject = Target;
>>>    }
>>>}
>>>
>>>public class ListClearedEventArgs : EventArgs
>>>{ 
>>>}
>>>
>>>
>>>
>>>Then in my other class I create the collection using a property:
>>>
>>>
>>>private Collection<MyRow> _Rows = new Collection< MyRow >();
>>>public Collection< MyRow > Rows
>>>{
>>>    get { return _Rows; }
>>>    set { _Rows = value;  }
>>>}
>>>
>>>
>>>and subscribe to the events this way:
>>>
>>>
>>>public MyList()
>>>{
>>>    InitializeComponent();
>>>
>>>    _Rows.ItemAdded += new Collection<MyRow>.EventHandler<EventArgs_Generic< MyRow >>(_Rows_ItemAdded);
>>>    _Rows.ItemRemoved += new Collection< MyRow >.EventHandler<EventArgs_Generic< MyRow >>(_Rows_ItemRemoved);
>>>    _Rows.Cleared += new Collection< MyRow >.ListClearedEventHandler(_Rows_Cleared);
>>>
>>>    _Setup = true;
>>>}
>>>
>>>
>>>The events are handled here:
>>>
>>>private void _Rows_ItemAdded(object sender, EventArgs_Generic< MyRow > e)
>>>{ 
>>>}
>>>private void _Rows_ItemRemoved(object sender, EventArgs_Generic< MyRow > e)
>>>{
>>>}
>>>private void _Rows_Cleared(object sender, ListClearedEventArgs e)
>>>{
>>>    Controls.Clear();
>>>}
>>>
>>>
>>>Then in the UI I call:
>>>
>>>
>>>MyList.Rows.Clear();
>>>
>>>
>>>When I step into the Clear method of the generic list, the Clear event is never called. When I put a breakpoint on the " if (Cleared != null)" line and step it from there, Cleared is NULL. Anyone see what's wrong here?
>>
>>Wire up the handlers using just:
 _Rows.ItemAdded += _Rows_ItemAdded;
>>_Rows.ItemRemoved += _Rows_ItemRemoved;
>>_Rows.Cleared += _Rows_Cleared;
>
>
>This is a rather small project. I just can't get the events to fire. If you're interested, the project is here :ftp://ftp.marois-consulting.com/
>User name is u56945964-FTPGuest and the PW is FTPGuest

Can log on but the download fails. Did you try the change I suggested. FWIW the following works for me (using the Generic class code exactly as you listed):
public class MyRow
{
}


public class Test
{
    private Collection<MyRow> _Rows = new Collection<MyRow>();
    public Collection<MyRow> Rows
    {
        get { return _Rows; }
        set { _Rows = value; }
    }

    public Test()
    {
        _Rows.ItemAdded += _Rows_ItemAdded;
        _Rows.ItemRemoved += _Rows_ItemRemoved;
        _Rows.Cleared += _Rows_Cleared;
    }

    public void TestAction()
    {
        Rows.Clear();
        Rows.Add(new MyRow());
        
    }

    private void _Rows_ItemAdded(object sender, EventArgs_Generic<MyRow> e)
    {
    }
    private void _Rows_ItemRemoved(object sender, EventArgs_Generic<MyRow> e)
    {
    }
    private void _Rows_Cleared(object sender, ListClearedEventArgs e)
    {

    }
}
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform