【问题标题】:Nested ItemsControls optimizations嵌套 ItemsControls 优化
【发布时间】:2016-04-20 04:27:36
【问题描述】:

我有一个关于在使用 XAML/C# 的 UWP 应用程序中使用嵌套集合的问题。 假设我有一个包含多个图像的项目列表。 我需要显示一个可滚动列表,其中包含项目数据中的所有图像。 到目前为止,我可以看到一个使用 ItemsTemplate 创建 GridView 的解决方案,其中包含 ItemsControl。但它的接缝速度非常慢且未优化解决方案。 有没有更好的建议来解决这个问题?

【问题讨论】:

    标签: c# xaml windows-runtime winrt-xaml uwp


    【解决方案1】:

    没有太多上下文,我只能想到以下内容:

    1. 您有大量数据要显示,但不是一次全部显示。在这种情况下,您应该考虑使用controls that support virtualization
    2. 在绑定数据源之前将其展平。这可能会稍微提高性能。

    更新

    你可以这样做:

    <ListViewItem ItemsSource="{Binding Posts}">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Title}" />
                    <TextBlock Text="{Binding Message}" />
    
                    <ScrollViewer HorizontalScrollBarVisibility="Auto">
                        <ItemsControl ItemsSource="{Binding Photos}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
    
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Image Source="{Binding}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ScrollViewer>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListViewItem>
    

    【讨论】:

    • 好吧,我相信我无法展平数据,因为它就像附有多张图片的 Facebook 帖子。关于虚拟化,我在一级列表上做你,但是如何虚拟化嵌套的itemscontrol呢?
    • 您实际上不需要虚拟化嵌套的ItemsControl,因为如果它们在可见区域之外,它们一开始就不会被实现。除非你有一个大相册。
    • 我已经在我的回答中包含了一个关于如何完成它的示例。
    • 我遇到的问题是,当使用嵌套项目控件时,UI 在创建和布局内部项目时会卡顿,即使每个项目只是一个带有图像的按钮。所以整体滚动性能很难看(例如,首先出现一个项目,然后调整大小以添加所有图像,每个项目都会发生这种情况。有没有办法优化这个过程,这样当我看到一个项目时在 ListView 内部它已经完全布局并准备好了?
    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2012-01-27
    • 2018-07-24
    • 2021-12-03
    相关资源
    最近更新 更多