0

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




        }


    }
Rom Shiri
  • 1,390
  • 4
  • 16
  • 29
  • Can you explain why you need so many PivotItems?. I wouldn't normally advise going against the guidelines so drastically, especially when it involves memory overflows – JayDev Jul 09 '14 at 21:25
  • 1
    I would suggest trying to use three pivots on the page and having a ManipulationDelta event to check if the user swiped to the left or the right. This method would mean you're only displaying 3 items at a time, the current item, the one before it and the one after it. A swipe left displays the next item and loads the next one while clearing the item of the pivot item 2 previously. Also use the Garbage collector in between loads and it should be a lot friendlier on memory. – Conway Stern Jul 10 '14 at 11:27
  • For testing remove the images from your items and check the memory consumption there. I assume you have an issue with the built-in image caching funtionality that causes your memory exception. If this is the case, see: http://stackoverflow.com/questions/18127027/memory-consumption-of-bitmapimage-image-control-in-windows-phone-8 – eX0du5 Jul 11 '14 at 08:25
  • Given that the `Pivot` control was not designed to do what you're looking for (it doesn't virtualise the `PivotItem` properly), have you tried any other controls? – Neil Turner Jul 13 '14 at 12:46
  • @eX0du5, I tried your advice, it still crash with memory out. however, after more items in the memeory. Still not good... NeilTurner, I use the pivot because I want to use the slide animation (while swiping) of it. DonaldDunlop, I tried your solution in many ways but the problem is that the Pivot.SelectedIndex changing while I add and remove items and by that it not corespond the item I wish to show. – Rom Shiri Jul 13 '14 at 17:20

0 Answers0