我想知道如何从事件中取消订阅匿名方法。
我已经在 C# 中检查了 Unsubscribe 匿名方法,但我的情况略有不同。
我正在匿名方法中访问局部函数变量。
代码如下
private static void Test(Object dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (dependencyPropertyChangedEventArgs.OldValue is ObservableCollection<object>)
{
(dependencyPropertyChangedEventArgs.OldValue as ObservableCollection<object>).CollectionChanged -=
(s, e) => SelectedItemsChanged(dependencyObject, e); // TO FIX event unbsubscription via anonymous delegate
}
if (dependencyPropertyChangedEventArgs.NewValue is ObservableCollection<object>)
{
(dependencyPropertyChangedEventArgs.NewValue as ObservableCollection<object>).CollectionChanged +=
(s, e) => SelectedItemsChanged(dependencyObject, e);
}
}