Run throguh visual tree and set all Images to null

前端 未结 1 382
攒了一身酷
攒了一身酷 2021-01-26 10:55

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, fin

相关标签:
1条回答
  • 2021-01-26 11:30

    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.

    0 讨论(0)
提交回复
热议问题