如何在基于 Windows 8 Xaml 的列表视图中禁用缓存
How to disable cache in Windows 8 Xaml based ListView?
我要做的是用预加载的图像动态填充Windows 8 Metro应用程序中的ListView。
对于每个项目 (URI),我用这样的代码非常简单地使用它 (C++):
Windows::UI::Xaml::Media::Imaging::BitmapImage^ bitmapSrc =
ref new Windows::UI::Xaml::Media::Imaging::BitmapImage();
bitmapSrc->CreateOptions = Windows::UI::Xaml::Media::Imaging::BitmapCreateOptions::IgnoreImageCache;
bitmapSrc->UriSource = uri;
img->Source = bitmapSrc;
LoadListView->Items->Append(img);
但是当我删除(在应用程序中)URI描述的源图像并创建具有相同名称的新文件并尝试将其重新加载到列表中时,我失败了,显示的图像是旧的(已删除)。我认为一些缓存在这里有效。我试图避免通过创建选项中的IgnoreImageCache值进行缓存,但它不起作用。
任何线索如何禁用可能绑定到Windows 8应用程序中ListView的BitmapSource(图像)的缓存?
我尝试了几个受Silverlight和WPF启发的方向,不幸的是,没有一个成功。
在评论的鼓励下,我把我找到的答案放在了自己身上。
下面解释了更广泛的上下文(以及 C# 视角):http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/171dfe66-78b5-4340-bd78-244337f31287/
很快,我认为这是参考计数的问题。只要 Uri 有效并分配给对象实例,WinRT 就会在 BitmapImage^ 中加载(缓存)图像,在我的示例中,该对象实例将添加到列表中。
在我的情况下,从 BitmapImage^ 中释放之前从 Uri 中清除 Uri 解决了问题。
根据相关示例,下面的代码解决了问题(包含在执行列表删除的部分):
auto item = (Image^)LoadListView->Items->GetAt(selected);
auto src = (Windows::UI::Xaml::Media::Imaging::BitmapImage^)item->Source;
src->UriSource = nullptr; //this line is critical
LoadListView->Items->RemoveAt(selected);
相关文章:
- 如何维护资源管理器项目视图中当前可见的项目列表
- 列表视图更改选择颜色
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 列表视图项的替代颜色
- 如何将 QString 的 QList 分配给列表视图?
- 如何修复列表视图中的错误?,封装控件时无法选择任何项
- 如何在 c++ 中从列表视图中获取值
- Qml 列表视图在重置模型时保留视图
- 创建列表视图后无法更改列表视图中的列宽
- 如何在调整列表视图大小时将标题宽度设置为等于其列表视图控件宽度
- 如何更改列表视图的边框颜色
- Qt:将 c++ 中的列表连接到 QML 中的列表视图
- 如何绑定到列表视图的 OnItemSelectionChanged
- 列表视图最初不显示数据
- 从C++访问列表视图委托的文本
- LVM_GETNEXTITEM在列表视图中找不到项目
- UWP 列表视图占用的高度超过可用高度
- 如何在移动列表视图中添加滚动条?并且用户只能垂直滚动特定区域
- C++ win32 列表视图列
- QML 列表视图和密钥导航 - 处理单个密钥事件