Ejemplo n.º 1
0
        public static void Main()
        {
            PopulatedOrganization wonkaFactory = new PopulatedOrganization();

            Organization chocolateDepartment = new Organization();

            #region SINGLE OBJECT EVENT BINDING

            // create the event binding for single object
            EventBinding<Organization, string> eventBinding =
                new EventBinding<Organization, string>();

            // specify the source object and the path to the source binding property that contains the event
            // we want to bind to
            eventBinding.SourceObj = wonkaFactory;
            eventBinding.SourcePathLinks =
                 StringCodePathResolver.ResolveStringPath("TheMostImportantDepartment").ToList();

            // do the binding itself
            eventBinding.Bind();

            // specify the event name (it can be done either before or after bind()
            // method is called
            eventBinding.EventName = "SomethingHappenedInOrgEvent";

            // add the event hanlder whose signature is similar to that of
            // SomethingHappenedInOrgEvent event to the binding
            eventBinding.TheEvent += eventBinding_SomethingHappenedInTheDepartment;

            // nothing is printed to the console because wonkaFactory.TheMostImportantDepartment
            // is still not set to chocolateDepartment object
            chocolateDepartment.FireSomethingHappenedEvent("Augustus Gloop went to the chocolate creek. (Before the department added - should not show)" );

            // set wonkaFactory.TheMostImportantDepartment
            wonkaFactory.TheMostImportantDepartment = chocolateDepartment;

            // this message is printed on the console
            chocolateDepartment.FireSomethingHappenedEvent("Augustus Gloop is out of the game (should show)");

            // unbind the event
            eventBinding.Unbind();

            #endregion SINGLE OBJECT EVENT BINDING

            #region COLLECTION EVENT BINDING

            // create the collection AllDepartments
            wonkaFactory.AllDepartments = new ObservableCollection<Organization>();

            // add chocolate department to it
            wonkaFactory.AllDepartments.Add(chocolateDepartment);

            // create collection event binding
            CollectionEventBinding<Organization, string> collectionEventBinding =
                new CollectionEventBinding<Organization, string>();

            // set the objects that contain the event we want to bind to
            // to be "AllDepartments" collection property of "wonkaFactory" object
            collectionEventBinding.SourceObj = wonkaFactory;
            collectionEventBinding.SourcePathLinks =
                 StringCodePathResolver.ResolveStringPath("AllDepartments").ToList();

            // bind the event
            collectionEventBinding.Bind();

            // set the event name (can be done before or after the binding)
            collectionEventBinding.EventName = "SomethingHappenedInOrgEvent";

            // add event handler
            collectionEventBinding.TheEvent += collectionEventBinding_TheEvent;

            // create gumDepartment
            Organization gumDepartment = new Organization();

            // fire an event (should not be handled since gumDepartment is not part of the collection yet)
            gumDepartment.FireSomethingHappenedEvent("We had great sales (Before the department is added - should not show)");

            // Add gum department to the collection
            wonkaFactory.AllDepartments.Add(gumDepartment);

            // fire the event (should be handled, since now gumDepartment is part of the collection)
            gumDepartment.FireSomethingHappenedEvent("We had great sales (After the department is added - should show)");

            // remove gum department from All Department collection
            // collectionEventBinding should be sufficiently smart to disconnect
            // the event of gumDepartment object from the handler
            wonkaFactory.AllDepartments.Remove(gumDepartment);

            // fire the event again - (the handler should not run, since gumDepartment has been removed from the collection)
            gumDepartment.FireSomethingHappenedEvent("We had great sales (After the department is Removed - should not show)");

            #endregion COLLECTION EVENT BINDING
        }