我要做的是使用预加载的图像动态填充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描述的源图像时,我创建了具有相同名称的新文件并尝试将其重新加载到列表中,然后我失败了,显示的图像是旧图像(已删除)。我假设一些缓存在这里工作。我试图避免在CreateOptions中使用IgnoreImageCache值进行缓存,但它不起作用。
如何禁用在Windows 8应用程序中可能绑定到ListView的BitmapSource(Image)的缓存?
我尝试了Silverlight和WPF启发的几个方向,但遗憾的是没有一个方向。
答案 0 :(得分:0)
鼓励评论,我回答说我找到了自己。
这里解释了更广泛的上下文(以及C#透视图): http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/171dfe66-78b5-4340-bd78-244337f31287/
很快我认为这是引用计数的问题。 只要Uri有效并且符合对象实例,WinRT就会将图像加载(缓存)在BitmapImage ^中,在我的示例中将其添加到列表中。
从BitmapImage ^中清除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);