问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/18886640/run-throguh-visual-tree-and-set-all-images-to-null