Windows Phone 8中列表框控件的项目限制

问题描述:

我在ScrollViewer控件下使用Listbox控件。我已经绑定了Listbox控件的项目。Windows Phone 8中列表框控件的项目限制

<ScrollViewer 
    Name="AgreementsSV" > 
    <ListBox 
     x:Name="MyAgr" 
     ItemsSource="{Binding MyAgreementList}" 
     SelectedIndex="-1" > 
     <Custom:Interaction.Triggers> 
      <Custom:EventTrigger EventName="Tap"> 
       <GalaSoft_MvvmLight_Command:EventToCommand 
       x:Name="AgreementTapCommand" 
       Command="{Binding NavigateToDetailsCommand, Mode=OneWay}" 
       CommandParameter="{Binding SelectedItem, ElementName=MyAgr}"/> 
      </Custom:EventTrigger> 
     </Custom:Interaction.Triggers> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 

      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</ScrollViewer> 

从后端我一次取50个项目,并将它们添加到列表中。 这工作正常,直到我达到列表中的600个项目。之后,应用程序崩溃。我没有发现任何异常。

所以我想确定是否有任何项目限制列表框控件或ScrollViewer?列表框控件有没有其他选择?

获取列表框数据的代码。

TotalRecordsExists = responseJson.total; 
foreach (Agreement item in responseJson.rows) 
{ 
    MyAgreementList.Add(item); 
    TotalRecordsFetched++; 
} 
if (TotalRecordsFetched < TotalRecordsExists) 
{ 
    PageNumber++; 
    GetMyAgreements(); 
} 
+0

你为什么在scrollviewer里添加你的列表框?列表框有一个滚动查看器本身。我建议你移除scrollviewer并重试。 – 2014-10-17 07:14:38

+0

也请在您从后端获取数据的函数中设置一些断点。后端可能会在600个项目后返回一些错误。请确认所有内容都正常运行,直到您将项目添加到集合中并且仅在此之后才发生崩溃。 – 2014-10-17 07:16:08

+0

为什么要将ItemsPanel设置为StackPanel? – 2014-10-17 07:17:01

不要将ListBox添加到ScrollViewer中。 ListBox ContentControl有它自己的内置滚动查看器应该很好。

试试这个代码: -

  <ListBox 
       x:Name="MyAgr" 
       ItemsSource="{Binding MyAgreementList}" 
       SelectedIndex="-1" > 
       <Custom:Interaction.Triggers> 
        <Custom:EventTrigger EventName="Tap"> 
         <GalaSoft_MvvmLight_Command:EventToCommand 
         x:Name="AgreementTapCommand" 
         Command="{Binding NavigateToDetailsCommand, Mode=OneWay}" 
         CommandParameter="{Binding SelectedItem, ElementName=MyAgr}"/> 
        </Custom:EventTrigger> 
       </Custom:Interaction.Triggers> 
       <ListBox.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel/> 
        </ItemsPanelTemplate> 
       </ListBox.ItemsPanel> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 

        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

此外,我不认为有必要为StackPanel的ItemsPanel在那里。

我觉得你在应用程序中遇到内存不足。不使用堆栈面板,而是使用默认进行数据虚拟化的virtualizedstackpanel。你也可以尝试使用LongListSelector而不是ListBox。