简体   繁体   English

Windows手机LonglistSelector无法呈现所有项目

[英]Windows phone LonglistSelector not rendering all items

I'm struggling with a longlistselector and item realized event. 我正在努力与longlistselector和项目实现事件。 The problem I'm facing is that the longlistselector does not show all elements. 我面临的问题是longlistselector没有显示所有元素。

The code I'm doing is not using MVVM (I know that I should use, but in this scenario I can't...it was heritage code). 我正在做的代码不是使用MVVM(我知道我应该使用,但在这种情况下我不能......它是遗产代码)。

This is what I have: 这就是我所拥有的:

XAML: 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 file: 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)
   {

   }
}

Right now my problem is that I know that allFiles has 96 elements, but only 67 are shown and the rest appear as white...any idea why? 现在我的问题是我知道allFiles有96个元素,但只显示了67个,其余的显示为白色...任何想法为什么?

EDIT I've update with the scrollviewer...because I've 3 longlistselectors in the same page...and only this last one doesn't show all the items. 编辑我用scrollviewer更新...因为我在同一页面中有3个longlistselectors ...而且只有最后一个没有显示所有项目。

The problem seems to be around the time with which the data is loaded (or the thread). 问题似乎是在加载数据(或线程)的时间。 The ItemRealised event happens on a Background Thread therefore isn't able to update the User interface. ItemRealised事件发生在后台线程上,因此无法更新用户界面。 In the example reference below they perform a similar operation to yours but retrieve the data using Deployment.Current.Dispatcher. 在下面的示例参考中,它们执行与您类似的操作,但使用Deployment.Current.Dispatcher检索数据。 This is used to do the work on the UI thread. 这用于在UI线程上完成工作。

Try something similar to the following: 尝试类似以下的内容:

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 TwitterSearch - Windows Phone 8 LongListSelector无限滚动示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM