小编Ill*_*zun的帖子

如何清除Windows Phone 7中的图像缓存.将UriSource设置为null不起作用!还有哪些其他变种可以清除WP7中的图像缓存?

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 ......

测试项目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)
  1. 运行您的应用程序 当我们第一次导航到MainPage.xaml时---内存值等于接近9676448字节
  2. 单击以导航到Caching.xaml(图像缓存)---内存值等于近9011200字节
  3. 单击按钮显示以显示图像 - 内存值等于近12996608字节
  4. 单击避免图像缓存(以清除缓存的图像),然后按下清除按钮--- 10090144字节....所以,哪里有消失413696字节??? (10090144 - 9676448 = 413696字节)
  5. 单击按钮返回导航回MainPage.xaml --- 11828248字节...但导航到Caching.xaml之前的前一个值是9676448字节...所以,在哪里消失了2151800字节????

    如果您将导航20次到页面Caching.xaml(单击show image并清除带缓存的图像),当导航回MainPage.xaml时---使用的内存将增加到3.3 MB ...依此类推

我在我的应用程序中遇到此问题,并且不知道如何解决它.

在Windows Phone 7中清除图像缓存还有哪些其他变体?或者为什么通过将UriSource设置为null来清除图像缓存---导航回上一页时不起作用(不释放内存)?

谢谢.

c# windows-phone-7

2
推荐指数
1
解决办法
3513
查看次数

标签 统计

c# ×1

windows-phone-7 ×1