为简单起见,我们有一个modelview(MyModelView),它包含一个Scheduler属性,里面有一个计时器(Scheduler)。计时器用于检查当前时间,如果它们匹配,则发出“事件匹配”信号,表示模型视图应以某种方式通知。现在MyModelView知道调度程序,但不是相反。
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;
我如何举起MyModelView可以订阅的活动?或者这是通过RelayCommanding完成的吗?
答案 0 :(得分:6)
您正在尝试向MyModelView公开的事件添加其他处理程序,而不是处理事件(这是无效的语法)。
您应该按照以下内容订阅活动:
public MyModelView() {
Scheduler scheduler = new Scheduler();
scheduler.HasEvents += OnHasEvents;
// ...
}
private void OnHasEvents(object sender, EventArgs e) {
// TODO: Do something here
}