【问题标题】:Coverflow with Out of Memory内存不足的 Coverflow
【发布时间】:2014-06-11 10:48:44
【问题描述】:

我正在开发 Windows 8 Phone 应用程序。

我有一个问题,我在图像顶部加载带有文本的图像。它称为覆盖流功能。

我收到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 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>

这里有大约 300 多个项目依次显示:

像这样:

它根本不工作我试图减少 widht and height from 400 to 200 它工作但我希望图像大小为 400 以便看起来不错。

即使我的图像是 400*400,我如何才能避免这种内存不足

【问题讨论】:

  • 一个想法是只加载你需要的这么多项目......某种分页。加载前 50 个左右...当用户滑动到末尾时再加载 25 个并删除前 25 个,依此类推...
  • @IvanCrojachKaračić 你能给我举个例子吗?

标签: c# silverlight windows-phone-8 windows-phone out-of-memory


【解决方案1】:

这真的是我的想法。好久没处理这个了。

1.给自己写一个函数,它会返回一堆项目

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

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

2.连接到SelectionChangedEvent 以供您控制

//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
     }
}

您还需要注意的另一件事是记住哪个是当前项目并将其再次设置为当前项目。

这一切都是从我的脑海中写出来的,我不知道它是否适用于你的控件,但我希望它至少能对你有所帮助:)

【讨论】:

  • 我所做的是我从Border 中删除了图像,而是添加了background as color with widht and height as 400,但它仍然崩溃,但如果我制作widht and height as 200,那么它可以工作,但我希望它是400. 这里可能有什么问题?我展示了大约 300 件商品
猜你喜欢
  • 2014-10-15
  • 2011-05-21
  • 1970-01-01
  • 2021-12-20
  • 2021-10-29
  • 1970-01-01
  • 2020-03-12
  • 2015-08-11
  • 2019-06-07
相关资源
最近更新 更多