[英]How to get ten, ten items from Observable Collection?
我試圖在Windows Phone 7列表框中每頁加載十個項目。 當我滾動到列表框的末尾時,應該加載另外10個項目。 我嘗試過一個簡單的刺痛。 我已經做到了。 當我嘗試加載我原來的可觀察集合時,我無法獲得10,10項。
我試過這樣的: -
void AddMoreItems()
{
int start = items.Count;
int end = start + 10;
for (int i = start; i < end; i++)
{
items.Add("Item " + i);
}
}
在這里,我每頁可以加載10個,10個項目。
現在我嘗試使用可觀察的集合: -
int end = 10;
int start = 0;
int total = listForLoading.Count;
void AddMoreItems()
{
if (total > 0)
{
int i = start;
foreach (var item in StudentDetails)
{
if (i < end)
{
items.Add(new ListBoxWithButtonModel() { FirstName = item.FirstName,LastName = item.LastName,Age = item.Age,PersonImage=item.PersonImage });
i++;
}
}
total = total > 10 ? total - 10 : total - total;
start = items.Count;
end = total > 10 ? start + 10 : start + total;
}
}
在這里,我一次又一次地獲得相同的物品。 我想在添加到列表框后從集合中刪除項目。 但我收到了錯誤。
請讓我知道從可觀察的集合中加載10,10個項目。
問題是foreach
總是從列表的開頭開始,但是你沒有在列表的開頭開始你的計數器。 您可以使用以下方法簡化代碼:
int taken = 0; // number of items already taken
int totalToTake = listForLoading.Count;
void AddMoreItems()
{
if (taken >= totalToTake) return; // all taken
int i = 0;
int stopi = taken+10;
foreach (var item in StudentDetails)
{
if (i >= taken && i < stopi)
{
// add your item here
// and then increment the number taken
taken++;
}
++i;
}
}
你可以用以下方法簡化:
var itemsToTake = StudentDetails.Skip(taken).Take(10);
foreach (var item in itemsToTake)
{
// add your item here
// and then increment the number taken
taken++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.