1

我要做的是用预加载的图像动态填充 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 启发的方向,但不幸的是没有一个有效。

4

1 回答 1

0

受到评论的鼓舞,我提出了我自己找到的答案。

此处解释了更广泛的上下文(以及 C# 视角):http: //social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/171dfe66-78b5-4340-bd78-244337f31287/

很快我相信这是引用计数的问题。只要 Uri 有效并分配给对象实例,WinRT 就会将图像加载(缓存)在 BitmapImage^ 中,在我的示例中将其添加到列表中。

在从列表中释放 Uri 之前从 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);
于 2012-12-11T10:40:38.163 回答