本文介绍了运行throguh visual tree并将所有Images设置为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.

这篇关于运行throguh visual tree并将所有Images设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:03