匿名方法给我们带来了方便,那么如何增加删除匿名方法中附加的委托(事件)呢
一般写法:
this.Loaded += (sender, e) => { //Do something };
进化写法:
this.Loaded += new RoutedEventHandler(FormulaAssign_Loaded); void FormulaAssign_Loaded(object sender, RoutedEventArgs e) { this.Loaded -= new RoutedEventHandler(FormulaAssign_Loaded); //do something }
优化写法:
RoutedEventHandler eventHander = null; eventHander = (object sender, RoutedEventArgs e) => { //do something
this.Loaded -= eventHander; }; this.Loaded += eventHander;