c# - CollectionChanged Event is not firing on a static ObservableCollection -


in 1 class i'm adding objects observablecollection. , in class, i'm doing stuff added object , delete collection.

those 2 classes cannot communicate each other, decided go static collection (i have access class definition reason)

in first class, elements added (i checked count property), in second class subscribe collectionchanged event. however, event not raising. think it's because of statickeyword, i'm not sure.

here code sample:

static public class { public static observablecollection<object> mycollection = new observablecollection<object>(); }  public class b { public b() { a.mycollection.collectionchanged += func_collectionchanged; }  void func_collectionchanged(...) {   //stuff } } public class c {    public void func()     {     a.mycollection.add(object);     } } 

here works fine me:

 class program     {         static void main(string[] args)         {             b obj = new b();           }     }     public class     {         public static observablecollection<object> mycollection = new observablecollection<object>();     }     public class b     {         public b()         {             a.mycollection.collectionchanged += func_collectionchanged;             a.mycollection.add(1);         }          private void func_collectionchanged(object sender, system.collections.specialized.notifycollectionchangedeventargs e)         {             // stuff here         }     } 

by using a.mycollection.collectionchangedline creating eventhandler handle the collection change event. fires when ever changes(add/update/delete) made in collection. since delegate creating have specify sender own event , type of arguments(what going handle), in-order proper reporting of published event

updates

you code. instance of class b not yet created, constructor of class automatically invoked when new instance of class created. creating event handler inside constructor of class b. not yet published event. reason collection_change event not triggering in code snippet.

hence definition class c following register event :

  public class c     {         b obj = new b();         public void func()     {               a.mycollection.add(1);     }     } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -