如何在 WindowsPhone 7 中清除图像缓存.将 UriSource 设置为 null 不起作用!在WP7中清除
本文关键字:清除 设置 null 不起作用 WP7 UriSource 缓存 WindowsPhone 图像 | 更新日期: 2023-09-27 18:30:42
>Stefan Wick在他的博客中写道,我们可以使用这样的代码清除WP7中的图像缓存
<code>
BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;
http://blogs.msdn.com/b/swick/archive/2012/04/05/10151249.aspx?CommentPosted=true#commentmessage
我已经测试了他的示例 - 项目(ImageTips.zip)---我可以说上面的代码不起作用,---它不会释放缓存图像的内存,也不会删除应用程序的缓存图像。
当您将 UriSource 设置为 null 时 - 您只释放页面 Caching.xaml 上的内存,但如果您将导航回 MainPage.xaml ---您将看到内存没有释放---内存增加了!!
要看到我们可以在他的博客中使用Stefan的项目---ImageTips.zip
...测试项目图像提示.zip
为了重现我的观察---您可以:1. 将代码添加到 MainPage.xaml 以查看当前内存值,就像在 Caching.xaml 页面上一样:
<tab>
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Start();
timer.Tick += delegate
{
GC.Collect();
tbMemory.Text = string.Format("Memory: {0} bytes", DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"));
};
</code>
- 运行应用程序。当我们第一次导航到 MainPage.xaml 时---内存值等于近 9676448 字节
- 单击以导航到 Caching.xaml(图像缓存)---内存值等于近 9011200 字节
- 单击按钮显示以显示图像 - 内存值等于近 12996608 字节
- 单击避免图像缓存(清除缓存的图像),然后按按钮清除--- 10090144字节....那么,哪里消失了413696字节???(10090144 - 9676448 = 413696 字节)
单击按钮返回以导航回主页.xaml --- 11828248字节...但是在导航到 Caching.xaml 之前的值是 9676448 字节...那么,哪里消失了2151800字节????
如果您将导航 20 次到页面 Caching.xaml(单击显示图像并清除带有缓存的图像),并且当导航回 MainPage.xaml 时---使用的内存将增加到 3.3 mb...等等
我在应用程序中遇到了这个问题,不知道如何解决。
在Windows Phone 7中清除图像缓存还有哪些其他变体?或者为什么在导航回上一页时通过将 UriSource 设置为 null --- 来清除图像缓存不起作用(不释放内存)?
谢谢。
我可以向你保证,博客中给出的例子是有效的。请注意,您在示例中处理的内存量非常小,无论您在这里或那里"丢失"了 2MB,都无关紧要。这可能是因为GC没有启动,或者在此期间分配了一些其他对象。(如果您确实遇到严重泄漏,则必须检查代码中的其他地方)
在我自己的应用程序中,我正在处理很多图像。在任何给定时间,它们最多消耗 60 MB 的内存。因此,我非常偏执地没有任何内存泄漏来通过认证。
博客中的示例假定您正在以编程方式添加图像,并且您有权访问它们,因此您可以将源设置为 null。
BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;
仅此一项就应该防止图像缓存。
但是,如果您使用的是数据绑定,您将很快意识到防止图像缓存可能并不容易。
在我使用数据绑定的应用程序中,我设置了类似于以下内容的内容:
假设您要在列表框中显示图像。
//MainPage.xaml
<StackPanel>
<ListBox ItemsSource="{Binding Pictures}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image MaxHeight="1000" Source="{Source}" />
</DataTemplate>
</ListBox.ItemTeplate>
</ListBox>
<Button Tap="LoadImages_Tap" Content="Load Images" />
</StackPanel>
-
//Picture.cs
public class Picture
{
private BitmapImage _source = new BitmapImage()
{
CreateOptions = BitmapCreateOptions.BackgroundCreation | BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.DelayCreation
};
public Picture(string url) {
_source.UriSource = new Uri(url, UriKind.Absolute);
}
public BitmapImage Source
{
get
{
return _source;
}
}
}
}
//MainPage.xaml.cs
private ObservableCollection<Picture> _pictures = new ObservableCollection<Picture>();
public ObservableCollection<Picture> Pictures {
get {
return _pictures;
}
}
public MainPage() {
//...
DataContext = this;
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
PreventCaching();
_pictures.Clear();
}
base.OnNavigatedFrom(e);
}
private void PreventCaching() {
foreach(var picture in _pictures) {
picture.Source.UriSource = null;
}
}
private void LoadPictures_Tap(object sender, EventArgs e) {
PreventCaching();
_pictures.Clear();
foreach(var url in SomeMethodThatReturnsUrlsForImages()) {
_pictures.Add(new Picture(url));
}
}
使用数据绑定时,上述内容应清除图像缓存(当无法直接访问
或者,您可以创建自定义附加依赖项属性,以便执行以下操作: