我有一个主流(IEnumerable),有超过100,000项。然后我控制了多个AutoCompleteBox。所有这些AutoCompleteBox都绑定到主流。
First:
现在的问题是,我的控制中有大约10个AutoCompleteBox,并且所有的ItemsSource都有主流作为它们的ItemsSource,从而导致了大量的内存占用。
第二版:
根据某些事件,我还需要在运行时对少数AutoCompleteBox的ItemsSource应用过滤器的功能。
我需要你的建议,以减少这种内存占用,并有一个功能应用过滤器的运行时。
发布于 2013-02-19 22:25:27
对于数据集合的内存占用情况,您应该查找顺序数据缓存
示例以减少内存消耗:
public void TestAutoCompleteLookup()
{
var path = "";
using (var c = SequentialDataCache<AutoCompleteItem>.Initialize())
{
path = c.Path;
//add 100.000 items
for (int i = 0; i < 100000; i++)
{
c.Add(new AutoCompleteItem() { Text = string.Format("{0}Text", i) });
}
//query
var pattern = "1";
var items = c.Where(autoCompleteItem => autoCompleteItem.Text.StartsWith(pattern)).ToArray();
}
if (File.Exists(path))
File.Delete(path);
}
[Serializable]
private class AutoCompleteItem
{
public string Text { get; set; }
}发布于 2012-02-23 13:37:55
内存占用
我相信这不仅取决于您的代码实现,还取决于AutocompleteControlas,以及它如何使用绑定数据源。
yield return实现yield return的计算属性,以便在运行时按需计算/返回项。VirtualizingStackPanel而不是StackPanel,这样框架将只为可见项分配内存和创建UI元素,而不是为所有绑定。在线过滤
看看MVVM方法。可以很容易地使用绑定到UI过滤器控件的属性来计算MainSTream项,基本上getter将使用公共绑定属性,而且每次更改这些属性时-- MainSTream将重新计算项并通过INotifyPropertyChanged通知UI,显然您需要实现对INotifyPropertyChanged的支持。请参阅WPF中对MVVM的一句解释?
public IEnumerable<IMyItem> MainStream
{
get
{
foreach(var item in mainDataSource)
{
if (item.Name == this.NameFilterBoundToUiTextBox)
{
yield return item;
}
}
}
}
private string nameFilter;
public string NameFilterBoundToUiTextBox
{
get
{
return this.nameFilter;
}
set
{
if (this.nameFilter != value)
{
this.nameFilter = value;
// TODO: Implement INotifyPropertyChanged
this.OnPropertyChanged("NameFilterBoundToUiTextBox");
// THis would notify UI to rebind MainSream
this.OnPropertyChanged("MainStream");
}
}
}https://stackoverflow.com/questions/9411915
复制相似问题