【发布时间】:2014-04-20 05:37:09
【问题描述】:
在我的 WP8 应用程序中,我有一个带有 lisbox 的页面,我将图像列表绑定到 ItemTemplate 中的其他数据。当我离开页面时,我觉得这些图像并没有从内存中释放出来。
以下是代码详情:
XAML
<ListBox x:Name="userList" ItemTemplate="{StaticResource DataTemplate1}" Tap="userList_Tap" Loaded="userList_Loaded">
<StackPanel Orientation="Horizontal" Width="220" Height="220" HorizontalAlignment="Center" VerticalAlignment="Center" >
<Image x:Name="episodeImage" HorizontalAlignment="Right" Height="120" Margin="0" VerticalAlignment="Top" Width="120" Source="{Binding DefaultImagePath}" />
<TextBlock x:Name="episodeName" HorizontalAlignment="Left" Margin="4,0,0,36" TextWrapping="Wrap" Width="Auto" Foreground="White" FontFamily="Segoe WP" Text="{Binding ImageName}" VerticalAlignment="Bottom"/>
</StackPanel>
</ListBox>
后面的C#数据:
public class ImageHolder{
public BitmapImage DefaultImagePath { get; set; }
public string ImageName { get; set; }
}
// list binding
List<ImageHolder> images=Utils.GetLargeImages();
userList.ItemSource=images;
public static List<ImageHolder> GetLargeImages(){
List<ImageHolder> images= new List<ImageHolder>();
for (int i = 0; i < 10; i++)
{
ImageHolder hold=new ImageHolder();
hold.ImageName=i+"";
hold.DefaultImagePath = new BitmapImage
{
DecodePixelWidth = 120,
DecodePixelHeight = 120,
UriSource = new Uri("Image_"+i+".png", UriKind.RelativeOrAbsolute) // this image is in 400x400 size
};
images.Add(hold);
}
return images;
}
我在(GetLargeImages() 方法)中获取图像期间使用了 DecodePixelWidth 和 DecodePixelHeight。
onnavigatedfrom 方法我将 null 设置为列表框。但是没有帮助,在几次进出页面后,我的应用程序因 OutofMemoryException 而崩溃。
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
userList.ItemsSource = null;
}
【问题讨论】:
-
能否将代码添加到 GetLargwImages 中?
-
用请求的方法更新了问题。
-
我已经浏览了链接,我知道如果单个图像控件重复用于加载多个图像,该解决方案是合适的。就我而言,我正在列表中加载多个图像 - 如何从内存中清除它们。你的意思是,在 OnNavigatedFrom 方法中用空图像重新分配我的列表框?
-
尝试为 ListBox 中的每个 BitmapImage 分配一个空的内存流,就像他在 DisposeImage 方法中所做的那样。看看这个解决方法是否有效
标签: c# xaml windows-phone-8