2

我正在开发 Windows Phone 8 应用程序。

我有 200 多个要显示的项目的列表框。

<DataTemplate x:Key="DataTemplate1">
            <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Border Grid.Row="0" Background="White" Height="400" Width="400" CornerRadius="30,30,30,30">
                </Border>
                <Grid Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Top">
                    <TextBlock HorizontalAlignment="Center" 
                               VerticalAlignment="Center"
                               Margin="5,20,5,5"
                               Foreground="#000000"
                               Text="{Binding Title}"/>
                </Grid>

            </Grid>
        </DataTemplate>

但它崩溃了,我已经调试了它直到 100 个它工作但之后它崩溃了。

PhoneApplicationPage_Loaded我有的方法中

private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
myList.Add(new MyObject("A","A value"));
            myList.Add(new MyObject("B", "B value"));
            myList.Add(new MyObject("C", "C value"));

and so on... 200 items

ListBoxItems.ItemsSource = myList;
}

我怎样才能解决这个问题 ?

更新 :

<ItemsPanelTemplate x:Key="ItemsPanelTemplate">
            <local:CollectionFlowPanel ItemHeight="400" 
                                       ItemWidth="400"
                                       FocusedItemOffset="120" 
                                       UnfocusedItemOffset="20" 
                                       ItemVisibility="5">
                <VirtualizingStackPanel />
            </local:CollectionFlowPanel>
        </ItemsPanelTemplate>
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" Background="#000000">
        <local:CollectionFlow x:Name="ListBoxItems"
                              ItemTemplate="{StaticResource DataTemplate}" 
                              ItemsPanel="{StaticResource ItemsPanelTemplate}"/>
    </Grid>
4

4 回答 4

3

确保您在列表框的 ItemsPanelTemplate 中有 VirtualizingStackPanel,请参阅此答案以获取更多信息

这是您的 ListBox 可能需要的 XAML:

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <VirtualizingStackPanel />
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>
于 2014-06-11T15:04:20.710 回答
1

您需要阅读 msdn 中关于列表和网格中数据可视化的以下博客。

通过列表或网格使用虚拟化

在没有看到你的整个 xaml 代码的情况下,我无法给出确切的答案,但我的猜测是你在 xaml ListBox 中被放置在画布/StackPanel 或滚动查看器控件中。

当 ItemsControl 的视口大小不受限制时,控件不会执行虚拟化。相反,它为其集合中的每个项目创建一个项目容器。一些不限制视口大小的常见容器是 Canvas、StackPanel 和 ScrollViewer。在这种情况下,您可以通过直接设置 ItemsControl 的大小来启用虚拟化,而不是让它由其父容器调整大小。在这里,我们在 GridView 上设置高度和宽度。这限制了视口的大小,并且视口之外的项目被虚拟化。

以下是两种情况,一种会抛出内存异常,另一种会正常工作(使用相同的代码并进行测试)

1. Canvas 中的 ListBox

 <Canvas .....
    <ListBox Name="ListBoxItems".....
    </ListBox>
 </Canvas>

上面的代码将抛出内存异常,因为项目控件的视口没有定义(如果你仍然想使用画布而不是定义宽度/高度,如果 ListBox 在这种情况下定义了项目控件的端口并且将应用虚拟化

2. Grid中的ListBox

  <Grid .....
    <ListBox Name="ListBoxItems".....
    </ListBox>
 </Grid>

由于在列表框上应用了虚拟化,上述代码不会抛出内存不足异常。

希望这会有所帮助

于 2014-06-11T19:32:22.883 回答
-1

你的对象有多大?如果您的对象太大,您可能无法一次加载所有对象。

于 2014-06-11T14:55:03.990 回答
-1

您是否尝试过使用 for 循环?

public List<Fellow> fellowList { get; set; }

private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
{

fellowList = new List<Fellow>();

for (int i = 0; i < 2; i++)
{
    Fellow fellow = new Fellow();
    fellow.x = "B" + i;
    fellow.value = "B Value" + i;
    fellowList.Add(fellow);
}
this.DataContext = this;

ListBoxItems.ItemsSource = fellowList;    

}

public class Fellow
{
public string x { get; set; }
public string value { get; set; }
}

希望它有所帮助..根据您的意愿更改视图模型

于 2014-06-11T15:07:54.013 回答