c# - 仅在可见时延迟加载 System.Windows.Control.Image

标签 c# wpf lazy-loading

我需要我的应用程序仅在图像对用户可见时才呈现图像。我试着附上。我尝试了以下 (f#):

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

但这只是加载,而不是延迟加载。 IsVisible 是如何工作的,只有当用户将图像元素滚动到 View 中时才会出现这种情况吗?

还尝试像这样修改绑定(bind)源:

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

            CaptureToWpfImage();
            return imageElement.Source;
        }
    }

如何才能让 BeginCapture() 只有在图像滚动到 View 中时才被调用?

最佳答案

听起来您需要支持虚拟化的东西。这只会在加载时创建可见元素。所有其他元素在可见时都是惰性创建的。

将 VirtualizingStackPanel 用于 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>

关于c# - 仅在可见时延迟加载 System.Windows.Control.Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4033741/

相关文章:

c# - 如何检查对象是否在主线程中创建

ios - UICollectionView visibleCells 在滚动前返回 0

c# - 在 ASP.Net MVC3 中使用 OpenID,我从哪里获取用户数据?

C# 2010 Express - 缺少指针

WPF 绑定(bind)到数据触发器中的父内容

java - Hibernate.initialize() 被完全忽略

c# - 如何使用 NHibernate Projections 检索集合

c# - 使用 JsonConverter 序列化/反序列化各种 `Brush` 类型

c# - 在包含两个位集的 C++ 结构上使用 C# 的 Marshal.PtrToStructure

c# - 从 WPF 应用程序获取应用程序的目录