Event consumption in WPF
- by webaloman
I have a very simple app written in Silverlight for Windows Phone, where I try to use events. In my App.xaml.cs code behind I have implemented a GeoCoordinateWatcher which registers a gCWatche_PositionChanged method. This works ok, method is called after the position has been changed. What I want to do is fire an other event lets say DBUpdatedEvent after DB has been updated in the gCWatche_PositionChanged method.
For this i delclared in the App.xaml.cs
public delegate void DBUpdateEventHandler(object sender, EventArgs e);
and I have in my App class:
   public event DBUpdateEventHandler DBUpdated;
the event is fired like this in the end of gCWatche_PositionChanged method like this:
       OnDBUpdateEvent(new EventArgs());
and also I have declared :
    protected virtual void OnDBUpdateEvent(EventArgs e)
    {
        if (DBUpdated != null)
        {
            DBUpdated(this, e);
        }
    }
Now I need to consume this event in my other Windows Phone app page which is a separate class PhoneApplicationPage.
So I declared this method in this other Phone Page:
    public void DBHasBeenUpdated(object sender, EventArgs e)
    {
        Debug.WriteLine("DB UPDATE EVENT CATCHED");
    }
And in the constructor of this page I declared:
        DBUpdateEventHandler dbEH = new DBUpdateEventHandler(DBHasBeenUpdated);
But when I test the application event is fired (OnDBUpdateEvent is called, but DBUpdated is null, therefore DBUpdated is not called - strange) and I have a problem that the other Phone Page is not catching the event at all...
Any suggestions? How to catch that event.
Thanks.