Stefan Wick在他的博客中写道,我们可以使用类似的代码清除WP7中的图像缓存
<code>
BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;
Run Code Online (Sandbox Code Playgroud)
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 ......
要重现我的观察结果---您可以: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>
Run Code Online (Sandbox Code Playgroud)
单击按钮返回导航回MainPage.xaml --- 11828248字节...但导航到Caching.xaml之前的前一个值是9676448字节...所以,在哪里消失了2151800字节????
如果您将导航20次到页面Caching.xaml(单击show image并清除带缓存的图像),当导航回MainPage.xaml时---使用的内存将增加到3.3 MB ...依此类推
我在我的应用程序中遇到此问题,并且不知道如何解决它.
在Windows Phone 7中清除图像缓存还有哪些其他变体?或者为什么通过将UriSource设置为null来清除图像缓存---导航回上一页时不起作用(不释放内存)?
谢谢.