嵌套ItemsControls优化

时间:2016-01-14 17:55:09

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

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

1 个答案:

答案 0 :(得分:0)

没有太多背景,我只能想到以下几点:

  1. 您需要显示大量数据,但不能同时显示所有数据。在这种情况下,您应该考虑使用controls that support virtualization
  2. 在绑定数据源之前展平数据源。这可能会稍微改善性能。
  3. <强>更新

    以下是如何做到这一点:

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