Skip to content

Commit 8c78745

Browse files
committed
修复未取消事件订阅导致的内存泄漏问题
1 parent afe3164 commit 8c78745

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

Firefly/Behaviors/AutoRowNumberBehavior.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,26 @@ public static void SetEnableAutoRowNumbers(DependencyObject obj, bool value)
2828

2929
private static void ItemsControl_Loaded(object sender, RoutedEventArgs e)
3030
{
31-
if (sender is not ItemsControl itemsControl)
31+
if (sender is not ItemsControl itemsControl || itemsControl.ItemsSource is not INotifyCollectionChanged notifyCollection)
3232
{
3333
return;
3434
}
3535

36-
if (itemsControl.ItemsSource is not INotifyCollectionChanged collection)
36+
void CollectionChangedHandler(object? s, NotifyCollectionChangedEventArgs args)
3737
{
38-
return;
38+
// DispatcherPriority.Render 确保 UI 更新后再执行 RefreshRowNumbers()
39+
itemsControl.Dispatcher.Invoke(() => RefreshRowNumbers(itemsControl), DispatcherPriority.Render);
40+
}
41+
42+
notifyCollection.CollectionChanged += CollectionChangedHandler;
43+
44+
void UnloadedHandler(object s, RoutedEventArgs args)
45+
{
46+
notifyCollection.CollectionChanged -= CollectionChangedHandler;
47+
itemsControl.Unloaded -= UnloadedHandler;
3948
}
4049

41-
// DispatcherPriority.Render 确保 UI 更新后再执行 RefreshRowNumbers()
42-
collection.CollectionChanged += (s, args)
43-
=> itemsControl.Dispatcher.Invoke(() => RefreshRowNumbers(itemsControl), DispatcherPriority.Render);
50+
itemsControl.Unloaded += UnloadedHandler;
4451

4552
RefreshRowNumbers(itemsControl);
4653
}

Firefly/Behaviors/AutoScrollBehavior.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void SetIsEnabled(DependencyObject element, bool value)
4444

4545
private static void ItemsControl_Loaded(object sender, RoutedEventArgs e)
4646
{
47-
if (sender is not ItemsControl itemsControl)
47+
if (sender is not ItemsControl itemsControl || itemsControl.ItemsSource is not INotifyCollectionChanged notifyCollection)
4848
{
4949
return;
5050
}
@@ -59,26 +59,31 @@ private static void ItemsControl_Loaded(object sender, RoutedEventArgs e)
5959
bool allowPause = GetAllowPause(itemsControl);
6060
bool autoScroll = true;
6161

62-
scrollViewer.ScrollChanged += (s, ev) => {
63-
if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
64-
{
65-
autoScroll = true;
66-
}
67-
else
62+
void ScrollChangedHandler(object s, ScrollChangedEventArgs args)
63+
{
64+
autoScroll = scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight || !allowPause;
65+
}
66+
67+
scrollViewer.ScrollChanged += ScrollChangedHandler;
68+
69+
void CollectionChangedHandler(object? s, NotifyCollectionChangedEventArgs args)
70+
{
71+
if (autoScroll && args.Action == NotifyCollectionChangedAction.Add)
6872
{
69-
autoScroll = !allowPause;
73+
scrollViewer.ScrollToEnd();
7074
}
71-
};
75+
}
7276

73-
if (itemsControl.ItemsSource is INotifyCollectionChanged notifyCollection)
77+
notifyCollection.CollectionChanged += CollectionChangedHandler;
78+
79+
void UnloadedHandler(object s, RoutedEventArgs args)
7480
{
75-
notifyCollection.CollectionChanged += (s, ev) => {
76-
if (autoScroll && ev.Action == NotifyCollectionChangedAction.Add)
77-
{
78-
scrollViewer.ScrollToEnd();
79-
}
80-
};
81+
scrollViewer.ScrollChanged -= ScrollChangedHandler;
82+
notifyCollection.CollectionChanged -= CollectionChangedHandler;
83+
itemsControl.Unloaded -= UnloadedHandler;
8184
}
85+
86+
itemsControl.Unloaded += UnloadedHandler;
8287
}
8388

8489
private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

0 commit comments

Comments
 (0)