Windows Phone 8 LongListSelector内存泄漏的图片

Thu*_*der 2 c# memory-leaks image longlistselector windows-phone-8

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

这是我的LongListSelector,它包含图像控件:

<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>
Run Code Online (Sandbox Code Playgroud)

在我的MainPage.xaml.cs中,我设置了LongListSelector的DataContext:

llsGameList.DataContext = gd.GetGamesListItems;
Run Code Online (Sandbox Code Playgroud)

这是我用来存储我的图像的类:

public class GetGamesList 
{
    public Uri BoxArtFrontThumb { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是包含所有图像的ObservableCollection:

 private ObservableCollection<GetGamesList> _GetGamesListItems = new ObservableCollection<GetGamesList>();
    public ObservableCollection<GetGamesList> GetGamesListItems
    {
        get
        {
            return this._GetGamesListItems;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我希望我能清楚地解释清楚.我真的希望有人能帮助我解决这个记忆问题.谢谢.

Kev*_*sse 5

我知道无法阻止LongListSelector泄漏内存.但是,您可以使用一个小技巧来释放图片使用的内存.

首先,创建一个名为的新类SafePicture,并使其继承ContentControl.在里面,实现逻辑来释放位图使用的内存:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,使用此自定义控件包装所有图片:

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,命名空间my引用您放入的程序集SafePicture,并且必须在页面顶部声明:

xmlns:my="clr-namespace:yourNamespace"
Run Code Online (Sandbox Code Playgroud)