Windows phone 8 图片 LongListSelector 内存泄漏 [英] Windows phone 8 Images inside LongListSelector memory leak
问题描述
我有一个 LongListSelector,它包含一个图像控件,它从 Web 加载大量图像,这在一段时间内工作正常,但在我加载一些图像后,出现内存不足异常.我读到其他人也有关于大量图像的内存不足的相同问题,但仍然没有找到解决方案.我读到它与图像/位图图像缓存有关.
I have a LongListSelector which contains a image control which loads a lot of images from the web, this works fine for some time, but after i loaded some images i get out of memory exception. I read other people having the same issue regarding out of memory with a lot of images but still haven't found a solution. I have read that it has something to do with image/BitmapImage cache.
这是我的 LongListSelector,其中包含图像控件:
Here is my LongListSelector which contains the image control:
<phone:Pivot Title="MY APPLICATION">
<!--Pivot item one-->
<phone:PivotItem Header="Browse">
<Grid>
<phone:LongListSelector Name="llsGameList" ItemsSource="{Binding}" Tap="llsGameList_Tap" Margin="0,90,0,0">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid>
<Image Name="imgGameList" Margin="0,10,0,10" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="150">
<Image.Source>
<BitmapImage UriSource="{Binding BoxArtFrontThumb}"
CreateOptions="BackgroundCreation" DecodePixelHeight="200" DecodePixelWidth="150" />
</Image.Source>
</Image>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
</phone:PivotItem>
在我的 MainPage.xaml.cs 中,我设置了 LongListSelector 的 DataContext:
In my MainPage.xaml.cs i set the DataContext of my LongListSelector:
llsGameList.DataContext = gd.GetGamesListItems;
这是我用来存储图像的类:
And here is the class i use to store my image in:
public class GetGamesList
{
public Uri BoxArtFrontThumb { get; set; }
}
这是包含所有图像的 ObservableCollection:
Here is the ObservableCollection containing all the images:
private ObservableCollection<GetGamesList> _GetGamesListItems = new ObservableCollection<GetGamesList>();
public ObservableCollection<GetGamesList> GetGamesListItems
{
get
{
return this._GetGamesListItems;
}
}
我希望我解释清楚.我真的希望有人可以帮助我解决这个内存问题.谢谢.
I hope i explained it clearly. I really hope there is someone that can help me fix this memory problem. Thanks.
推荐答案
我知道没有办法防止 LongListSelector 泄漏内存.不过,你可以用一个小技巧来释放图片使用的内存.
I know no way to prevent the LongListSelector from leaking memory. However, you can use a small trick to free the memory used by pictures.
首先,创建一个名为SafePicture
的新类,并使其继承自ContentControl
.在里面,实现释放位图使用的内存的逻辑:
First, create a new class, called SafePicture
, and make it inherit from ContentControl
. Inside, implement the logic to free the memory used by the bitmap:
public class SafePicture : System.Windows.Controls.ContentControl
{
public SafePicture()
{
this.Unloaded += this.SafePictureUnloaded;
}
private void SafePictureUnloaded(object sender, System.Windows.RoutedEventArgs e)
{
var image = this.Content as System.Windows.Controls.Image;
if (image != null)
{
image.Source = null;
}
}
}
然后,使用此自定义控件包装所有图片:
Then, wrap all your pictures using this custom control:
<phone:Pivot Title="MY APPLICATION">
<!--Pivot item one-->
<phone:PivotItem Header="Browse">
<Grid>
<phone:LongListSelector Name="llsGameList" ItemsSource="{Binding}" Tap="llsGameList_Tap" Margin="0,90,0,0">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid>
<my:SafePicture>
<Image Name="imgGameList" Margin="0,10,0,10" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="150">
<Image.Source>
<BitmapImage UriSource="{Binding BoxArtFrontThumb}"
CreateOptions="BackgroundCreation" DecodePixelHeight="200" DecodePixelWidth="150" />
</Image.Source>
</Image>
</my:SafePicture>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
</phone:PivotItem>
请注意,命名空间 my
指的是您将 SafePicture
放入的程序集,并且必须在页面顶部声明:
Note that the namespace my
refers to the assembly you've put the SafePicture
in, and must be declared on top of your page:
xmlns:my="clr-namespace:yourNamespace"
这篇关于Windows phone 8 图片 LongListSelector 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!