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:
01444023
Vues:
31
>>>>>>>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)
>>>>    {
>>>>
>>>>    }
>>>>}
>>>
>>>Yes, I did try the change. No luck. The project is only 113K, so I zipped & emailed it to ya. I could use some help if you have a moment.
>>
>>The Email only contained the source for Marois.Common.XControls.XLinkList.Collection< T > (_Collection.cs). Looks exactly as the code you posted - and also works with the test code that I posted earlier (ie. the event handlers run when the TestAction() method executes. Try sending the complete project again if you want.....
>>IAC the problem must lie in your MyList class
>
>I renamed the item names in my original post. The solution I sent is it.
>
>In the demo form if you break in the Clear button's click event and step from there you'll immediatly go to the Collection's Clear method:
>
>
>public void Clear()
>{
>    _List.Clear();
>    _Count = 0;
>
>    if (Cleared != null)
>    {
>        ListClearedEventArgs args = new ListClearedEventArgs();
>        Cleared(this, args);
>    }
>}
>
>
>In mine, Cleared is null. I'm curious to see what happens for you.

Your problem is here:
       public Collection<XLinkRow> Rows
        {
            get { return _Rows; }
            set 
            { 
                _Rows = value;
                _LoadRows();
            }
        }
You wire up the event handlers to an instance of Collection< XLinkRow > then replace it with another one. Needs a redesign really but you can use this instead:
public Collection<XLinkRow> Rows
        {
            get { return _Rows; }
            set 
            { 
                  _Rows.Clear();
                foreach (XLinkRow x in value)
                    Rows.Add(x);
            }
        }
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform