我目前在WPF TreeView上有一个奇怪的内存泄漏。当我在 TreeView 中选择一个项时,相应的绑定 ViewModel 在 TreeView EffectiveValueEntry[] 集合中被强力保存。问题是,当 ViewModel 从其父集合中删除时,它不会发布。
这是重现问题的简单代码:
MainWindow.xaml
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls.Primitives;
namespace TreeViewMemoryLeak
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public ObservableCollection<Entry> Entries
{
get
{
if (entries == null)
{
entries = new ObservableCollection<Entry>() { new Entry() { DisplayName = "First Entry" } };
}
return entries;
}
}
private void Button_Click(object sender, RoutedEventArgs e) { entries.Clear(); }
private ObservableCollection<Entry> entries;
}
public class Entry : DependencyObject
{
public string DisplayName { get; set; }
}
}
MainWindow.xaml.cs
<Window x:Class="TreeViewMemoryLeak.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TreeViewMemoryLeak"
Title="MainWindow" Height="350" Width="250">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Entry}">
<TextBlock Text="{Binding DisplayName}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<Button Content="delete item" Click="Button_Click" Grid.Row="0" Margin="10"/>
<TreeView x:Name="treeView" ItemsSource="{Binding Entries}" Grid.Row="1" Margin="10" BorderBrush="Black" BorderThickness="1" />
</StackPanel>
</Window>
重现问题
选择项目,然后单击按钮以清除"可观察集合"。现在检查 TreeView 控件上的 EffectiveValueEntry[] :ViewModel 仍然存在,并且没有标记为垃圾回收。
我终于想出了一个相当暴力的解决方案。在删除树视图中的最后一个对象时,我自己从有效值集合中删除了引用。这可能矫枉过正,但至少,它有效。
public class MyTreeView : TreeView
{
protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
{
base.OnSelectedItemChanged(e);
if (Items.Count == 0)
{
var lastObjectDeleted = e.OldValue;
if (lastObjectDeleted != null)
{
var effectiveValues = EffectiveValuesGetMethod.Invoke(this, null) as Array;
if (effectiveValues == null)
throw new InvalidOperationException();
bool foundEntry = false;
int index = 0;
foreach (var effectiveValueEntry in effectiveValues)
{
var value = EffectiveValueEntryValueGetMethod.Invoke(effectiveValueEntry, null);
if (value == lastObjectDeleted)
{
foundEntry = true;
break;
}
index++;
}
if (foundEntry)
{
effectiveValues.SetValue(null, index);
}
}
}
}
protected MethodInfo EffectiveValueEntryValueGetMethod
{
get
{
if (effectiveValueEntryValueGetMethod == null)
{
var effectiveValueEntryType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(t => t.Name == "EffectiveValueEntry").FirstOrDefault();
if (effectiveValueEntryType == null)
throw new InvalidOperationException();
var effectiveValueEntryValuePropertyInfo = effectiveValueEntryType.GetProperty("Value", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance);
if (effectiveValueEntryValuePropertyInfo == null)
throw new InvalidOperationException();
effectiveValueEntryValueGetMethod = effectiveValueEntryValuePropertyInfo.GetGetMethod(nonPublic: true);
if (effectiveValueEntryValueGetMethod == null)
throw new InvalidOperationException();
}
return effectiveValueEntryValueGetMethod;
}
}
protected MethodInfo EffectiveValuesGetMethod
{
get
{
if (effectiveValuesGetMethod == null)
{
var dependencyObjectType = typeof(DependencyObject);
var effectiveValuesPropertyInfo = dependencyObjectType.GetProperty("EffectiveValues", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance);
if (effectiveValuesPropertyInfo == null)
throw new InvalidOperationException();
effectiveValuesGetMethod = effectiveValuesPropertyInfo.GetGetMethod(nonPublic: true);
if (effectiveValuesGetMethod == null)
throw new InvalidOperationException();
}
return effectiveValuesGetMethod;
}
}
#region Private fields
private MethodInfo effectiveValueEntryValueGetMethod;
private MethodInfo effectiveValuesGetMethod;
#endregion
}
您将树视图与OneTime
模式绑定,因此您的集合已"快照"。如前所述:
更新:
EffectiveValueEntry
是关于DependencyObjects
如何存储其DependencyProperties
的值。只要 treeView 具有选定的项,此集合将保存对象。一旦您选择其他内容,集合就会更新。
我遇到了同样的问题,并使用此link
的解决方案之一解决了它(由汤姆·戈夫发布)。执行以下操作:
ClearSelection(this.treeView);
this.treeView.SelectedValuePath = ".";
this.treeView.ClearValue(TreeView.SelectedValuePathProperty);
this.treeView.ItemsSource = null;
。
public static void ClearSelection(TreeView treeView)
{
if (treeView != null)
ClearSelection(treeView.Items, treeView.ItemContainerGenerator);
}
private static void ClearSelection(ItemCollection collection, ItemContainerGenerator generator)
{
if ((collection != null) && (generator != null))
{
for (int i = 0; i < collection.Count; i++)
{
TreeViewItem treeViewItem = generator.ContainerFromIndex(i) as TreeViewItem;
if (treeViewItem != null)
{
ClearSelection(treeViewItem.Items, treeViewItem.ItemContainerGenerator);
treeViewItem.IsSelected = false;
}
}
}
}