2
爲了簡單起見,有一個模型視圖(MyModelView),其中包含一個Scheduler屬性,它內部有一個計時器(Scheduler)。計時器用於檢查當前時間,如果它們匹配,則表示應該通知模型視圖的'事件匹配'。現在MyModelView知道調度器,但不是相反。WPF MVVM事件訂閱
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'只能出現上的+ =或左手側 - =(除型內使用時)
如何將我提出了一個MyModelView可以訂閱的事件?或者這是通過RelayCommanding完成的嗎?
太謝謝你了 – Casey