wpf 延迟加载列表视图中的可见项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21319143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lazy-loading visible items in a Listview
提问by Tokfrans
I have a listview which uses the following code:
我有一个使用以下代码的列表视图:
<ListView x:Name="Display" ItemsSource="{Binding}" Background="#373737" Margin="0,0,350,0" BorderThickness="0" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="767" Height="88">
<Border Height="64" Width="64" Margin="12,12,0,12">
<Image Source="{Binding Path=album.albumart}" Stretch="UniformToFill"/>
</Border>
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="0,10,0,0">
<TextBlock Text="{Binding Path=name}"
Margin="10,0,0,0" Width="300" Height="40"
TextTrimming="WordEllipsis" TextWrapping="Wrap" FontSize="16" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Path=album.name}"
Margin="10,-15,0,0" Width="300" Height="20"
TextTrimming="WordEllipsis" HorizontalAlignment="Left"
FontSize="14" Opacity="0.49"/>
<TextBlock Text="{Binding Path=artistname}"
Margin="10,2,0,0" Width="300"
TextTrimming="WordEllipsis" HorizontalAlignment="Left"
FontSize="12" Opacity="0.49"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And I have about 400 objects with images (this takes quite a bit of memory)
我有大约 400 个带有图像的对象(这需要相当多的内存)
Which are then displayed in each listviewitem.
然后显示在每个列表视图项中。
Is there any way for the listview to tell items to load their image from a cache I have based on which objects are visible in the listview instead of having all the images loaded all the time, which, as previously said takes quite a bit of memory.
有什么方法可以让列表视图告诉项目从缓存中加载它们的图像,我基于哪些对象在列表视图中可见,而不是一直加载所有图像,如前所述,这需要相当多的内存.
Hope you guys understand what I'm on about, thank you.
希望大家明白我的意思,谢谢。
回答by Lorentz Vedeler
I tried this solution with my pictures folder containing more than 3500 pictures in high resolution. Memory usage peaked at 120MB with furious scrolling which seemed to trigger garbage collection and reduced memory to about 50MB. I don't know if that's sufficiently low memory usage?
我使用包含 3500 多张高分辨率图片的图片文件夹尝试了此解决方案。内存使用量在 120MB 时达到峰值,剧烈滚动似乎触发了垃圾收集并将内存减少到大约 50MB。我不知道这是否足够低的内存使用量?
<ListBox ItemsSource="{Binding Images}" VirtualizingPanel.IsVirtualizing="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Height="64" Width="64">
<Image.Source>
<BitmapImage
DecodePixelHeight="64"
DecodePixelWidth="64"
UriSource="{Binding Path=., Mode=OneWay,UpdateSourceTrigger=Explicit}"
CreateOptions="DelayCreation"
CacheOption="None" />
</Image.Source>
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ViewModel:
视图模型:
public class ViewModel : INotifyPropertyChanged
{
public ICollectionView Images { get; private set; }
public ViewModel()
{
}
public void LoadImages()
{
var folder = @"C:\Users\lrved_000\Pictures";
var photos = System.IO.Directory.EnumerateFiles(folder, "*.jpg",SearchOption.AllDirectories);
Images = CollectionViewSource.GetDefaultView(photos);
RaisePropertyChanged("Images");
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}

