Windows phone 8 ListBox在加载大量数据后变空

时间:2014-06-05 14:26:32

标签: c# xaml windows-phone-8

在我的WP8应用程序中,我有一个执行Lazy加载的列表框。一切都很好,除了一件事。有时向上或向下滚动后,即使数据相同,我的列表框也会变空。我使用“Windows手机应用程序分析”分析了已用内存,并获得了最大使用内存100MB。所以记忆没有问题。在列表框变空之后,aplication也不会崩溃。你知道为什么会这样吗?

这是我的XAML:

<ListBox Name="lbCollections" LayoutUpdated="lbCollectins_LayoutUpdated">
              <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Background="White" Margin="20">
                        <Grid Margin="15,5,15,5">                                
                            <Grid.RowDefinitions>
                                <RowDefinition Height="45"></RowDefinition>
                                <RowDefinition Height="30"></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Row="0" Text="{Binding  CategoryName}" Foreground="Black" Style="{StaticResource EmptyListHeaderStyle}"></TextBlock>
                            <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding CreatedOn, StringFormat='{}{0:MM dd, yyyy}'}" Foreground="Gray" Style="{StaticResource PhoneTextSmallStyle}"></TextBlock>
                            <TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding PhotosCountTxt}" Foreground="Gray" Style="{StaticResource PhoneTextSmallStyle}"></TextBlock>
                        </Grid>
                        <Grid Height="150">
                            <Grid.Resources>
                                <conv:ImageConverter x:Key="ImageConverter" />
                            </Grid.Resources>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition></ColumnDefinition>
                                <ColumnDefinition></ColumnDefinition>
                                <ColumnDefinition></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Image Grid.Column="0" Name="img1" Source="{Binding Photos, Converter={StaticResource ImageConverter}, ConverterParameter=0}" Width="150" Height="150" Margin="5"/>
                            <Image Grid.Column="1" Name="img2" Source="{Binding Photos, Converter={StaticResource ImageConverter}, ConverterParameter=1}" Width="150" Height="150" Margin="5"></Image>
                            <Image Grid.Column="2" Name="img3" Source="{Binding Photos, Converter={StaticResource ImageConverter}, ConverterParameter=2}" Width="150" Height="150" Margin="5"></Image>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>            

这是背后的代码:

private async void LoadContent()
    {
        if (loadingProgress)
        {
            return;
        }
        else
        {
            try
            {
                loadingProgress = true;
                CollectionResponse collectionResp = await CollectionService.IterateAsync(MapPoint, "", dtStart, dtEnd, filterCategoryId, loadedCollectionsCount, collectionsCountToLoadAtOnce, 4, (int)TimeZoneInfo.Local.BaseUtcOffset.TotalMinutes, AppSettings.Invisible, AppSettings.Token);
                if (collectionResp.Collections.Count == 0)
                {
                    endOfList = true;
                }
                else
                {
                    loadedCollectionsCount += collectionResp.Collections.Count;
                    if (!(lbCollections.ItemsSource is List<CollectionModel>))
                    {
                        lbCollections.ItemsSource = new List<CollectionModel>();
                    }
                    (lbCollections.ItemsSource as List<CollectionModel>).AddRange(collectionResp.Collections);
                    //lbCollections.ItemsSource = collectionResp.Collections;
                }                    
            }
            catch
            {

            }
            finally
            {
                loadingProgress = false;
            }
        }

    }

    private void lbCollectins_LayoutUpdated(object sender, EventArgs e)
    {            
        ScrollBar scrollBar = FindChildOfType<ScrollBar>(lbCollections).Where(sb=>sb.Orientation == System.Windows.Controls.Orientation.Vertical).FirstOrDefault();
        if (scrollBar != null)
        {
            bool endOfScroll = scrollBar.Maximum > 0 && scrollBar.Value > 0 && scrollBar.Maximum - scrollBar.Value < 10;
            if (!endOfList && endOfScroll)
            {
                LoadContent();
            }
        }            
    }

    static List<T> FindChildOfType<T>(DependencyObject root) where T : class
    {
        List<T> foundObjects = new List<T>();
        var queue = new Queue<DependencyObject>();
        queue.Enqueue(root);

        while (queue.Count > 0)
        {
            DependencyObject current = queue.Dequeue();
            for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; i >= 0; i--)
            {
                var child = VisualTreeHelper.GetChild(current, i);
                var typedChild = child as T;
                if (typedChild != null)
                {
                    foundObjects.Add(typedChild);
                }
                queue.Enqueue(child);
            }
        }
        return foundObjects;
    }

1 个答案:

答案 0 :(得分:0)

要做的两件事。

  1. 查看this blog有关ListBox为空的原因。最有可能的问题是您正在显示的图像。您可以通过执行以下操作轻松修复

                         

  2. 开始使用适用于WP8的LongListSelector(不是WP Toolkit版本!)它专为快速滚动而设计,即使是图像