我必须创建一个“无限”的图像卷,让我们说一些类似于Facebook的内容。我使用一个存储我的条目的列表框,为此我使用以下代码。
foreach (var newsPiece in feedDataArray){
StackPanel entry_holder = new StackPanel();
entry_holder.Background = new SolidColorBrush(Color.FromArgb(255, 28, 95, 166));
entry_holder.Width = 410;
entry_holder.Margin = new Thickness(0, 10, 0, 20);
entry_holder.Tag = newsPiece.entryId;
TextBlock entry_title = new TextBlock();
Run titleFragment = new Run();
titleFragment.Text = newsPiece.posterName;
titleFragment.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
Run titleFragment2 = new Run();
titleFragment2.Text = " " + newsPiece.title;
titleFragment2.Foreground = new SolidColorBrush(Color.FromArgb(255, 155, 192, 231));
Run titleFragment3 = new Run();
titleFragment3.Text = newsPiece.withString;
titleFragment3.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
Run titleFragment4 = new Run();
if (!(newsPiece.locationName == null))
{
if (newsPiece.locationName.Length > 5) {
titleFragment4.Text = " at " + newsPiece.locationName + ".";
}
}
else {
titleFragment4.Text = ".";
}
titleFragment4.Foreground = new SolidColorBrush(Color.FromArgb(255, 155, 192, 231));
entry_title.Width = 395;
entry_title.TextWrapping = TextWrapping.Wrap;
entry_title.Margin = new Thickness(5, 5, 5, 0);
entry_title.TextAlignment = TextAlignment.Left;
entry_title.FontSize = 20;
entry_title.FontFamily = new System.Windows.Media.FontFamily("Segoe WP Light");
entry_title.FontWeight = FontWeights.Light;
entry_title.Inlines.Add(titleFragment);
entry_title.Inlines.Add(titleFragment2);
entry_title.Inlines.Add(titleFragment3);
entry_title.Inlines.Add(titleFragment4);
entry_holder.Children.Add(entry_title);
Image entry_image;
BitmapImage tempBit;
Uri imageURI = new Uri(newsPiece.imageURL);
entry_image = new Image();
entry_image.Width = 400;
entry_image.Margin = new Thickness(5, 7, 7, 0);
entry_image.Stretch = Stretch.UniformToFill;
tempBit = new BitmapImage(imageURI);
tempBit.DecodePixelWidth = 400;
entry_image.Source = tempBit;
entry_holder.Children.Add(entry_image);
TextBlock entryFooter = new TextBlock();
entryFooter.Width = 300;
entryFooter.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
entryFooter.Margin = new Thickness(10, 0, 0, 0);
entryFooter.Text = newsPiece.likesNumber + " Likes " + newsPiece.commentsNumber + " Comments";
entryFooter.Foreground = new SolidColorBrush(Color.FromArgb(255, 155, 192, 231));
TextBlock entryFooter2 = new TextBlock();
entryFooter2.Width = 170;
entryFooter2.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
entryFooter2.Margin = new Thickness(10, -25, 5, 0);
string date = System.DateTime.Now.ToString("yyyy-MM-dd");
if (date == newsPiece.date)
{
entryFooter2.Text = newsPiece.time;
}
else {
entryFooter2.Text = newsPiece.date + " " + newsPiece.time;
}
entryFooter2.Foreground = new SolidColorBrush(Color.FromArgb(255, 155, 192, 231));
entryFooter2.TextAlignment = TextAlignment.Right;
entry_holder.Children.Add(entryFooter);
entry_holder.Children.Add(entryFooter2);
feedEntries.Items.Add(entry_holder);}
基本上我将标题图像和日期插入到列表框项目中。问题是每次用户到达列表底部时我都要加载更多的newsPieces而且我不想删除之前加载的项目,但是在插入20个项目之后,在低RAM设备(如Lumia 520)上,它内存耗尽。有什么优化的想法吗?谢谢。
您可以忽略代码的文本操作部分,只有在我加载图像时才会出现问题。 循环:
Image entry_image;
BitmapImage tempBit;
Uri imageURI = new Uri(newsPiece.imageURL);
entry_image = new Image();
entry_image.Width = 400;
entry_image.Margin = new Thickness(5, 7, 7, 0);
entry_image.Stretch = Stretch.UniformToFill;
tempBit = new BitmapImage(imageURI);
tempBit.DecodePixelWidth = 400;
entry_image.Source = tempBit;
entry_holder.Children.Add(entry_image);