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