通过可视化树运行并将所有图像设置为空 [英] Run throguh visual tree and set all Images to null
本文介绍了通过可视化树运行并将所有图像设置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到大量线程在使用图像时出现内存泄漏.那么,仅仅拥有一个通用函数,某种自己的 GC,它会在 NavigatingFrom 上运行,找到所有图像(甚至在虚拟列表的模板中)并将它们设置为 null,是不是一个好主意?
I saw tons of threads with memory leaking while using images. So, is it a good idea just to have a general function, some kind of own GC, which would run at NavigatingFrom, find all images (even in templates of virtualized lists) and set them to null?
推荐答案
这里有一个帮助器来遍历页面的所有图像:
Here is an helper to iterate throught all the images of your page:
public IEnumerable<Image> GetAllImage(DependencyObject root)
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child is Image)
{
yield return (Image)child;
}
foreach (var image in GetAllImage(child))
{
yield return image;
}
}
}
您可以将页面的根作为参数调用它,它应该将所有图像返回给您.
You can just call it with the root of your page as parameter and it should return all the images to you.
这篇关于通过可视化树运行并将所有图像设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文