Windows phone LonglistSelector不呈现所有项

本文关键字:phone LonglistSelector Windows | 更新日期: 2023-09-27 18:04:45

我正在与longlistselector和item实现事件作斗争。我面临的问题是,longlistselector不显示所有元素。

我正在做的代码是不使用MVVM(我知道我应该使用,但在这种情况下我不能…

这是我的:

XAML:

    <Scrollviewer>
<stackpanel>
        <phone:LongListSelector Margin="0,15,0,0"  ScrollViewer.VerticalScrollBarVisibility="Visible" x:Name="LBhistory" LayoutMode="List"  
BorderThickness="0,15,0,0" >
        <phone:LongListSelector Margin="0,15,0,0"  ScrollViewer.VerticalScrollBarVisibility="Visible" x:Name="LBDevices" LayoutMode="List"  BorderThickness="0,15,0,0" >
        <phone:LongListSelector Margin="0,15,0,0"  ScrollViewer.VerticalScrollBarVisibility="Visible" x:Name="LBfiles" LayoutMode="List"  BorderThickness="0,15,0,0" >
</stackpanel>
    </ScrollViewer>

CS文件:

private bool _isLoadingAllFile;
private int _pageNumber = 0;
private ObservableCollection<PhotoObject> allFiles = new ObservableCollection<PhotoObject>();
public BackupPivotPage()
{
   ....
   this.Loaded += PivotPage_Loaded;
}
private void PivotPage_Loaded(object sender, RoutedEventArgs e)
{
   LBfiles.ItemsSource = allFiles;
   LBfiles.ItemRealized += LBfiles_ItemRealized;
   searchImages(_pageNumber++);
}
private void searchImages(int p)
{
   _isLoadingAllFile = true;
   var x = dbAllFiles.Skip(p * GlobalSettings.PageSize.myPictures)
              .Take(GlobalSettings.PageSize.myPictures);
   foreach (var toAddObject in x)
   {
      this.allFiles.Add(toAddObject);
   }
   _isLoadingAllFile = false;
}
void LBfiles_ItemRealized(object sender, ItemRealizationEventArgs e)
{
   try
   {
      if (!_isLoadingAllFile && LBfiles.ItemsSource != null &&
          LBfiles.ItemsSource.Count >= Constants.offsetKnob)
      {
         if (e.ItemKind == LongListSelectorItemKind.Item)
         {
            if ((e.Container.Content as PhotoObject)
               .Equals(LBfiles.ItemsSource[LBfiles.ItemsSource.Count - Constants.offsetKnob]))
            {
               searchImages(this._pageNumber++);
            }
         }
      }
   }
   catch (Exception e1)
   {
   }
}

现在我的问题是,我知道allFiles有96个元素,但只有67个显示,其余的显示为白色…知道为什么吗?

编辑我更新了滚动查看器…因为我在同一个页面上有3个长列表选择器…只有最后一个没有显示所有项

Windows phone LonglistSelector不呈现所有项

问题似乎在加载数据(或线程)的时间附近。itemrealize事件发生在后台线程中,因此无法更新用户界面。在下面的示例引用中,它们执行与您的操作类似的操作,但使用Deployment.Current.Dispatcher检索数据。这用于在UI线程上完成工作。

尝试下面类似的内容:

Try
{
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
       var x = dbAllFiles.Skip(p * GlobalSettings.PageSize.myPictures)
              .Take(GlobalSettings.PageSize.myPictures);
       foreach (var toAddObject in x)
       {
          this.allFiles.Add(toAddObject);
       }
       IsLoading = false;
    });
}
catch (Exception e)
{
   Deployment.Current.Dispatcher.BeginInvoke(() =>
   {
    MessageBox.Show("Network error occured " + e.Message);
   });
}

TwitterSearch - Windows Phone 8 LongListSelector Infinite Scrolling Sample