C# LongListSelector内存泄漏中的Windows phone 8图像
我有一个LongListSelector,它包含一个图像控件,可以从web加载很多图像,这在一段时间内工作正常,但在加载一些图像后,我的内存出现异常。我读到其他人也有同样的问题,关于很多图像的内存不足,但仍然没有找到解决方案。我读到它与image/BitmapImage缓存有关 这是我的LongListSelector,其中包含图像控件:C# LongListSelector内存泄漏中的Windows phone 8图像,c#,image,windows-phone-8,memory-leaks,longlistselector,C#,Image,Windows Phone 8,Memory Leaks,Longlistselector,我有一个LongListSelector,它包含一个图像控件,可以从web加载很多图像,这在一段时间内工作正常,但在加载一些图像后,我的内存出现异常。我读到其他人也有同样的问题,关于很多图像的内存不足,但仍然没有找到解决方案。我读到它与image/BitmapImage缓存有关 这是我的LongListSelector,其中包含图像控件: <phone:Pivot Title="MY APPLICATION"> <!--Pivot item one-->
<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>
下面是我用来存储图像的类:
public class GetGamesList
{
public Uri BoxArtFrontThumb { get; set; }
}
以下是包含所有图像的ObservableCollection:
private ObservableCollection<GetGamesList> _GetGamesListItems = new ObservableCollection<GetGamesList>();
public ObservableCollection<GetGamesList> GetGamesListItems
{
get
{
return this._GetGamesListItems;
}
}
private ObservableCollection_getGameSlitiems=new ObservableCollection();
公共可观测集合GetGameSlities
{
得到
{
返回此。\u获取游戏列表;
}
}
我希望我能解释清楚。我真的希望有人能帮我解决这个记忆问题。谢谢。我不知道如何防止长列表选择器泄漏内存。但是,您可以使用一个小技巧来释放图片使用的内存 首先,创建一个名为
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;
}
}
}
然后,使用此自定义控件包装所有图片:
<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>
你可以试试这个:@KooKiz这个方法能完全消除图像内存缓存问题吗?@KooKiz-ohh我看到你写到它不能完全消除内存泄漏。难道没有办法不泄露任何内存吗?据我所知没有。但是这个技巧会处理使用更大内存量的东西:图片。控件本身使用的内存几乎可以忽略不计。谢谢。你能告诉我如何在我的代码中实现它吗,然后我会我会把你的答案标记为已接受:)。谢谢你,伙计:)。我很快就会测试这个。这个修正对你自己也有效吗?当你在任何应用程序中使用它时?。好的,我实现了代码,但是我应该如何调用代码?代码是否自动调用?@Thunder它是自动调用的。如果您有疑问,可以在
SafePictureUnloaded
方法中设置断点。是的,我在我的应用程序中使用它,这是我发现的唯一可靠的处理内存泄漏的方法。再次感谢。我现在测试了它,我认为它正在减少内存使用:)。但是每次内存都会增加一点,是LongListSelector中的一个bug导致了这种情况吗?。如果我使用listbox控件,你认为它会更好吗?你不会有listbox的漏洞,但你也不会有虚拟化,这将是一个显示大量图片的大问题。Telerik控件在有限的时间内是免费的,您应该抓住这个机会:它们的RadDataBoundListBox支持虚拟化,并且不会泄漏内存
<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>
xmlns:my="clr-namespace:yourNamespace"