Coverflow with Out of Memory

2019-03-06 01:15发布

问题:

i am working on Windows 8 Phone Application.

I have issue where i am loading image with text on top of the images one by one.Its called coverflow feature.

I am getting Out of memory exception

for (int j = 0; j < items.Count; j++)
            {
                for (int i = 0; i < items.Collection.Count; i++)
                {
                    Myobj obj = items[j].Collection[i];
                    if (obj.correct == 1)
                    {
                        coverflow.Add(new CoverFlow(items[j].Text, answer.TextTwo));
                    }
                }
            }

            CarouselList.ItemsSource = coverflow;

DataTemplate :

<DataTemplate x:Key="DataTemplate1">
            <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Border Grid.Row="0" Height="400" Width="400" CornerRadius="30,30,30,30">
                    <Border.Background>
                        <ImageBrush ImageSource="Images/sample.png" />
                    </Border.Background>
                </Border>
                <Grid Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Top">
                    <TextBlock HorizontalAlignment="Center" 
                               VerticalAlignment="Center"
                               Margin="5,20,5,5"
                               Foreground="#000000"
                               Text="{Binding Title}"/>
                </Grid>
                <Grid Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Bottom">
                <TextBlock HorizontalAlignment="Center" 
                               VerticalAlignment="Center"
                               Margin="5,5,5,20"
                               Foreground="#000000"
                               Text="{Binding SubTitle}"/>
                </Grid>
            </Grid>
        </DataTemplate>

Here the there are around 300+ items that displays one after the other:

Like this :

Its not working at all i tried to reduce the widht and height from 400 to 200 it works but i want the image size to 400 so that it looks good.

how can i avoid this Out Of Memory even if my images are 400*400

回答1:

This is going to be really from the top of my head. Haven't dealt with this in a while.

1.Write yourself a function which will return you a bunch of items

public List<Item> GetFirstItems()
{
    return items.Collection.Take(50);
}

public Item GetOtherItems(int skip)
{ 
     return items.Collection.Skip(skip).Take(25)
}

2.Hook up to the SelectionChangedEvent for your control

//keep this somewhere so you know where you are in the list
var currentBatch = 0;

private void SelectionChanged(sender object, ChangedEventArgs e)
{
     var position = e.CurrentItemIndex % 25;

     if(position > currentBatch)
     {
          currentBatch = position;
          var newItems = GetOtherItems(currentBatch * 25);

          //take the global list of items and modify it;
          //because we are moving right we only need the last 25 so we
          //can skip the first 25
          coverflow= coverflow.Skip(25);

          //add your new items
          coverflow.AddRange(newItems);
          CarouselList.ItemsSource = coverflow; // you will have to clear the source first
     }
     else if(position < currentBatch)
     {
          currentBatch = position;
          var newItems = GetOtherItems(currentBatch * 25);

          //take the global list of items and modify it;
          //because we are moving left we only need the first 25 so we
          //can take the first 25
          coverflow= coverflow.Take(25);

          //add your new items
          newItems.AddRange(coverflow);
          coverflow = newItems;
          CarouselList.ItemsSource = coverflow; // you will have to clear the source first
     }
}

One more thing you will have to take care of is memorizing which was the current item and setting it again to be the current item.

This is all written from the top of my head and I have no idea if it work with your control but I hope it will at least help you a bit :)