darthmaule2
Topic Author
Posts: 89
Joined: 23 Oct 2014, 19:54

Any way to know if a UIElement is visible in the window?

21 Aug 2019, 21:01

I have a ListView bound to an ObservableCollection of 100s of items, each of which has an associated Noesis.BitmapImage that must be displayed as a thumbnail on the screen. I can't load them all at once or the UI thread pauses for a long time. So, I only want to load the ones that are visible in the screen when scrolling stops.

How do I check if a UIElement is visible? (The IsVisible property returns true even for offscreen elements).

I found the below code somewhere but it uses "TransformBounds" which doesn't appear to be implemented in Noesis...
        public static bool IsUserVisible(this UIElement element)
        {
            if (!element.IsVisible)
                return false;
            var container = VisualTreeHelper.GetParent(element) as FrameworkElement;
            if (container == null) throw new ArgumentNullException("container");

            Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.RenderSize.Width, element.RenderSize.Height));
            Rect rect = new Rect(0, 0, container.ActualWidth, container.ActualHeight);
            return rect.IntersectsWith(bounds);
        }


Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 1682
Joined: 22 Dec 2011, 19:20

Re: Any way to know if a UIElement is visible in the window?

22 Aug 2019, 20:12

Is the BitmapImage exposed as a property in each item view model?

Considering a ListView uses virtualization to render the items (so only visible ones are generated in the UI side), you can maybe delay the loading of the images to the first time the Image property is requested:
public class Item
{
  public Noesis.BitmapImage Image
  {
    get
    {
      if (_image == null)
      {
        _image = new Noesis.BitmapImage(_imagePath);
      }
      return _image;
    }
  }
  ...
}
Do you think this could work for you?

Who is online

Users browsing this forum: No registered users and 2 guests