लीक करने पर वर्तमान में WPF TreeView के साथ एक अजीब स्मृति रिसाव है। जब मैं TreeView में कोई आइटम चुनता हूं, तो इसी बाउंड व्यू मॉडेल को दृढ़ता से TreeView EffectiveValueEntry [] संग्रह में पकड़ लिया जाता है। मुद्दा यह है कि जब इसे ViewModel को इसके मूल संग्रह से हटा दिया जाता है तो इसे जारी नहीं किया जाता है।WPF TreeView चयनित आइटम
यहाँ समस्या को ठीक करने के लिए एक सरल कोड है:
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>
समस्या को पुनः करने के लिए
आइटम का चयन करें, फिर ObservableCollection को साफ़ करने के लिए बटन क्लिक करें। अब TreeView नियंत्रण पर EffectiveValueEntry [] की जांच करें: ViewModel अभी भी वहां है और कचरा संग्रह के लिए फ़्लैग नहीं किया गया है।
क्या नेट संस्करण आप उपयोग कर रहे: निम्नलिखित है? – JleruOHeP
मुझे .NET 3.5 और 4.0 के साथ समस्या है। मैं पूरी तरह से इसका जिक्र करना भूल गया, क्षमा करें। मैं अभी 4.5 के साथ परीक्षण करूंगा। – Sisyphe
समस्या अभी भी .NET 4.5 – Sisyphe