简体   繁体   English

删除绑定后,Image.Source(带有绑定)正在阻止图像文件

[英]Image.Source (with binding) is blocking image file after binding is removed

In a GridView , I've templated the items so an Image control inside that template receives an Uri using a binding (also tried with BitmapImage) to show a picture file. GridView ,我已经对项目进行了模板化,因此该模板中的Image控件使用绑定(也尝试使用BitmapImage)来接收Uri来显示图片文件。 It works, but I cannot delete those files because those files are blocked. 它可以工作,但是我无法删除这些文件,因为这些文件已被阻止。 Even if I clear the collection feeding the GridView.ItemsSource , and even if I call GC.Collect() , they are still blocked. 即使我清除提供GridView.ItemsSource的集合,并且即使我调用GC.Collect() ,它们也仍然被阻止。

Thanks to Philip for his working suggestion... as I used a Converter to feed a BitmapImage to every Image control, found some particular things had to do to make it work, and here it is for future reference: 感谢Philip的工作建议...当我使用Converter将BitmapImage馈送到每个Image控件时,发现需要做一些特殊的事情才能使其工作,在这里供以后参考:

InMemoryRandomAccessStream Ras = new InMemoryRandomAccessStream();
var archivo = CartoonsDownloader.FolderImagenes.GetFileAsync(TheFileName);

var fileStream = CartoonsDownloader.FolderImagenes.OpenStreamForReadAsync(TheFileName);
fileStream.Result.CopyTo(Ras.AsStreamForWrite());

BitmapImage MapaDeBits = new BitmapImage();
// Even it's RANDOM, I have to manually "Seek" it at 0.
Ras.Seek(0);
MapaDeBits.SetSource(Ras);
MapaDeBits.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
return MapaDeBits;

This could be a bug in the XAML stack (so you could report it on MSDN forums ), or just a side effect of its nature of caching images. 这可能是XAML堆栈中的错误(因此您可以在MSDN 论坛上进行报告),​​或者只是其缓存图像性质的副作用。 As an alternative - you can open the file yourself and set the image using the SetSource method. 或者,您可以自己打开文件并使用SetSource方法设置图像。 Then you should have better control over the file access. 然后,您应该更好地控制文件访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM