Windows Phone 8里面的LongListSelector内存泄漏图片 [英] Windows phone 8 Images inside LongListSelector memory leak

查看:136
本文介绍了Windows Phone 8里面的LongListSelector内存泄漏图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LongListSelector,它包含一个从Web加载大量图像的图像控件,这个工作正常一段时间,但在我加载一些图像后,我的内存异常。我读过其他人对于内存不足以及大量图片有相同问题,但仍然没有找到解决方案。我已经读过它与image / BitmapImage缓存有关。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆