For simplicity have a modelview (MyModelView) that contains a Scheduler property which has a timer inside of it (Scheduler). The timer is used to check against the current time, and if they match, signal an ‘event match’ that the modelview should somehow be notified of. Now MyModelView knows about the scheduler, but not the other way around.
public Scheduler()
{
ScheduleCollection = new ObservableCollection<Schedule>();
TimeSpan ts = new TimeSpan(30000);
_timer = new DispatcherTimer();
_timer.Interval = ts;
_timer.Tick += new EventHandler(EventTimerCheck_Tick);
_timer.Start();
}
private void EventTimerCheck_Tick(object sender, EventArgs e)
{
eventsToLaunch = LocateCurrentEvents();
if (eventsToLaunch.Count > 0) { RaiseHasEvents(); }
}
public void RaiseHasEvents()
{
EventHandler handler = this.HasEvents;
if (handler != null)
{
var e = new EventArgs();
handler(this, e);
}
}
public event EventHandler HasEvents;
public MyModelView()
{
Scheduler scheduler = new Scheduler();
HaveEvents += scheduler.HasEvents; <----*throws Error below
}
internal event EventHandler HaveEvents;
- Scheduler.HasEvents’ can only appear on the left hand side of += or -= (except when used from within the type)
How would I raise an event that the MyModelView can subscribe to? Or is this accomplished through RelayCommanding ?
You are trying to add additional handlers to an event exposed by your MyModelView, not handle the event (which is not valid syntax).
You should be doing something like the following to subscribe to the event: