简体   繁体   中英

Lazy loading System.Windows.Control.Image only when visible

I need my application to render an image only when it becomes visible to the user. I tried attaching. I've tried the following (f#):

   image.IsVisibleChanged.Add(fun e ->
        if image.IsVisible & mtvCapture.Capture <> null then
            mtvCapture.BeginCapture()
        )

But this just loads, doesn't lazy load. How does IsVisible work, will this only be true when the users scrolls the image element into view?

Also tried modifying the binding source like so:

    public ImageSource ImageElementSource
    {
        get
        {
            if (Capture == null)
            {
                BeginCapture();
                return loadingImageSource;
            }

            CaptureToWpfImage();
            return imageElement.Source;
        }
    }

How can I have BeginCapture() be called only when image is scrolled into view?

Sounds like you need something that supports Virtualization. This only creates the visible elements at load time. All other elements are created lazy when they get visible.

Example using VirtualizingStackPanel for a ListBox

<ListBox Name="c_imageListBox">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding ImagePath}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM