From: Xenomorph <xen...@on...> - 2004-05-06 22:25:52
|
Am Do, den 06.05.2004 schrieb Bryan Bulten um 22:59: > However, there is one disadvantage. If you try adding a handler more > than once, only the last handler added gets called. For example: > > Clicked += new EventListener(Test); > Clicked += new EventListener(Test2); > > When the event is triggered, only 'Test2' will get called. The same > disadvantage exists with the existing event system. I have already found a solution for this... I added a struct called SListeners. So, instead of adding only the EventListener to the ArrayList listeners I add the struct to it. Now, when you call AddCommandListener like this AddCommandListener(Event.wxEVT_COMMAND_BUTTON_CLICKED, ID, value, this); it also adds a reference of the calling object to the arraylist listeners. When an event calls back MarshalEvent, it tries to find equal objects in the list and calls all! delegates, not only the last added one. Alexander -------------- EvtHandler.cs-------------------- namespace wx { public delegate void EventListener(object sender, Event e); public struct SListener { public EventListener listener; public Object owner; public SListener( EventListener listener, Object owner ) { this.listener = listener; this.owner = owner; } } public class EvtHandler : Object { --- code removed --- //--------------------------------------------------------------------- public void AddCommandRangeListener(int eventType, int id, int lastId, EventListener listener, Object owner) { // I must keep a reference to the listener to prevent it from // being garbage collected. I had trouble passing the listener // delegate into C and back (.NET threw a runtime error, Mono // crashed) so I pass the index into the listeners array instead. // Works like a charm so far. //listeners.Add(listener); listeners.Add( new SListener(listener, owner) ); wxEvtHandler_Connect(wxObject, eventType, id, lastId, listeners.Count - 1); } //--------------------------------------------------------------------- public void AddCommandListener(int eventType, int id, EventListener listener, Object owner) { AddCommandRangeListener(eventType, id, -1, listener, owner); } ---- code removed --- //--------------------------------------------------------------------- // All listened-for events are received here. The event code is // mapped to an actual Event type, and then the listener EventListener lsnrtion // is called. private void MarshalEvent(IntPtr wxEvent, int iListener) { // Create an appropriate .NET wrapper for the event object Event e = Event.CreateFrom(wxEvent); // Send it off to the registered listener //EventListener listener = (EventListener)listeners[iListener]; SListener listener = (SListener)listeners[iListener]; foreach ( SListener sl in listeners ) { // continue if listener equals sl, because it will be handled below if ( listener.Equals( sl ) ) continue; if ( sl.owner != null ) if ( listener.owner != null ) if ( sl.owner.Equals( listener.owner ) ) { sl.listener(this, e); } } listener.listener(this, e); } ------------------------------------------------ |