我有一個情況,我想要一個給定的事件執行一次,只有一次。我試圖這樣做,但我有問題(否則我不會問)。請注意,以下代碼位於決定需要觸發事件的函數內部。使c#/ wpf事件只觸發一次?
EventHandler h = delegate(Object sender, EventArgs e) {
FiringControl.TheEvent -= h; // <-- Error, use of unassigned local variable "h"
// Do stuff
}
FiringControl.TheEvent += h;
一般來說,這應該的,因爲範圍保存代表,直到後他們就完成了運行方式工作,但由於h
是正在興建的過程中,當我嘗試使用它,它仍然被認爲是顯然是「未初始化」。
http://stackoverflow.com/questions/1554845/am-i-using-the-right-approach-to-monitor-the-tasks-i-want-to-perform-when-a-handl/1554978 #1554978 – Juliet 2010-06-03 18:39:56