ListBox 数据虚拟化未生效 [英] ListBox data virtualization is not taking effect

查看:18
本文介绍了ListBox 数据虚拟化未生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 1000 个来自 xml 的项目并将它们加载到一个 List 对象中.列表数据绑定到水平方向的 ListBox,因此用户可以从左到右或从右到左翻阅项目.由于项目数量巨大,我的应用程序可能由于内存使用过多而退出.如果我将项目减少到 50,它就起作用了.

I had 1000 items from xml and loaded them in a List object. List is databound to ListBox which is horizontally oriented so user can flip through items left to right or right to left. Since number of items are huge my app was quitting probably due to excessive memory usage. If I reduced the items to 50 it worked.

我找到了这篇文章http:///shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx

然后是这篇关于数据虚拟化的文章

and then this article on data virtualization

http://blogs.msdn.com/b/ptorr/archive/2010/08/16/virtualizing-data-in-windows-phone-7-silverlight-applications.aspx

在实现了一个实现 IList 的虚拟化类之后,我看不出有什么区别.this[](下面)仍然被调用了 1000 次,尽管我预计它只会被调用 30-40 次,因为我知道 UI 已经在 Listbox 中进行了虚拟化.为什么虚拟化没有启动?

After implementing a virtualized class implementing IList I see no difference. The this[] (below) is being called 1000 times still though I expected to it be called only 30-40 times since I understand UI is already virtualized in Listbox. Why is virtualization not kicking in?

object IList.this[int index]
{
    get
    {
        if (index >= cachedItems.Count)
        {
            //replenish cache code here
        }

        return cachedItems[index];
    }
    set
    {
        throw new NotImplementedException();
    }
}

这是与问题相关的 XAML 部分.希望这给出了代码的全貌.不确定 Width=Auto 是否与它有关,但我无法更改它,否则我的滑动会停止.

Here is the XAML portion relevant to the problem. Hope this gives the full picture of the code. Not sure if Width=Auto has anything to do with it but I can't change it otherwise my swiping stops.

<ScrollViewer HorizontalScrollBarVisibility="Auto" Margin="0,0,0,0" Width="auto" x:Name="WordsScrollview" Opacity="1"  Grid.Row="1" RenderTransformOrigin="0.5,0.5">

    <ListBox x:Name="horizontalListBox" Width="auto" Height="Auto" >

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal">
                </StackPanel>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Width="430" Text="{Binding Word}"  TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" TextAlignment="Center" />
                    <Image Height="290" HorizontalAlignment="Center" Name="image1" Stretch="Fill"  Width="430" Source="{Binding ImageFile}" Margin="10,50,10,0" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

        <ListBox.Background>
            <SolidColorBrush />
        </ListBox.Background>

    </ListBox>

</ScrollViewer>

推荐答案

这是导致 UI 虚拟化最终启动的 XAML.

Here is the XAML that is causing UI Virtualization to finally kick in.

        <ListBox x:Name="horizontalListBox"   Height="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" >

                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Horizontal">

                        </VirtualizingStackPanel>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>

                            <StackPanel>
                                <TextBlock Width="430" Text="{Binding Word}"  TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" TextAlignment="Center" />

                                <Image Height="290" HorizontalAlignment="Center" Name="image1" Stretch="Fill"  Width="430" Source="{Binding ImageFile}" Margin="10,50,10,0" />
                               </StackPanel>

                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.Background>
                    <SolidColorBrush />
                </ListBox.Background>
            </ListBox>

这篇关于ListBox 数据虚拟化未生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆