我正在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>

最佳答案

确保列表框see this answer for more info的ItemsPanelTemplate中有VirtualizingStackPanel。

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

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

09-07 04:27