I have a pivot control that its ItemsSource is databound to a collection of items.
The collection can be up to 50 items, so it means that it can be up to 50 PivotItems. Each PivotItem contains Image control and some text controls.
After swiping between the pivot pages (about 15), when pressing the back button, the app crashs with "Out of Memory" exception.
Ofcourse the problem is in the PivotItems that takes all the memory each time I swipe to a new one.
I know the guildlines not recommend to use pivot with more than 6-7 items, but I really need it that way.
I saw some answer on the web that advices to use ContentControl with Content binded to my item layout as Resource.. but I find it hard to Implement.
Any more suggestions how to achieve the desirable result (no memory overflow)?
Thanks!
Edit:
I tried to manipulate the items by taking only 3, the current, the next and the previous. But the behavior of the pivot is not good.. How should I increment/decrement the Pivot.SelectedIndex?
private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
{
if (e.Direction.ToString() == "Horizontal") //Left or right
{
var itemVM = this.DataContext as ItemViewModel;
int i = pivot.SelectedIndex;
ObservableCollection<Item> items = new ObservableCollection<Item>();
var currentItemIndex = itemVM.CurrentReader.Reader.Items.IndexOf(itemVM.SelectedItem);
if (e.HorizontalVelocity > 0) //Right
{
currentItemIndex++;
}
else //Left
{
currentItemIndex--;
}
if (itemVM.AllItems.Count >= 3)
{
if (currentItemIndex == 0) // in case we are in the first item m in the AllItems collection.
{
items.Add(itemVM.AllItems[itemVM.AllItems.Count - 1]);
items.Add(itemVM.AllItems[currentItemIndex]);
items.Add(itemVM.AllItems[currentItemIndex + 1]);
}
else if (currentItemIndex == itemVM.AllItems.Count - 1) // in case we are in the last item in the AllItems collection.
{
items.Add(itemVM.AllItems[currentItemIndex - 1]);
items.Add(itemVM.AllItems[currentItemIndex]);
items.Add(itemVM.AllItems[0]);
}
else
{
items.Add(itemVM.AllItems[currentItemIndex - 1]);
items.Add(itemVM.AllItems[currentItemIndex]);
items.Add(itemVM.AllItems[currentItemIndex + 1]);
}
}
else // in case we have only 3 items, no manpulation needed.
{
items = itemVM.AllItems;
}
itemVM.CurrentItemsCollection = items; // only 3 items
}
}