c# – 在Listview中加载延迟可见项
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 在Listview中加载延迟可见项,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2426字,纯文字阅读大概需要4分钟。
内容图文
![c# – 在Listview中加载延迟可见项](/upload/InfoBanner/zyjiaocheng/778/2f23e0bef8da47f9aa7b27c5b0038556.jpg)
我有一个listview,它使用以下代码:
<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>
我有大约400个图像对象(这需要相当多的内存)
然后在每个listviewitem中显示哪些内容.
列表视图是否有任何方法可以告诉项目从缓存中加载图像我基于哪些对象在列表视图中可见而不是一直加载所有图像,如前所述需要相当多的内存.
希望你们明白我的意思,谢谢.
解决方法:
我用我的图片文件夹尝试了这个解决方案,其中包含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>
视图模型:
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));
}
}
}
内容总结
以上是互联网集市为您收集整理的c# – 在Listview中加载延迟可见项全部内容,希望文章能够帮你解决c# – 在Listview中加载延迟可见项所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。