如何在基于 Windows 8 Xaml 的列表视图中禁用缓存

How to disable cache in Windows 8 Xaml based ListView?

本文关键字:视图 列表 缓存 Xaml Windows      更新时间:2023-10-16

我要做的是用预加载的图像动态填充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);