清除由<img/>创建的图像缓存

时间:2015-07-14 07:10:07

标签: windows-runtime windows-store-apps windows-phone-8.1

如果我在Windows应用商店或Windows Phone应用中有这样的元素:

<Image Source="{Binding UrlToWebServer}" />

图像在本地缓存。这很棒。但是如何从代码中删除光盘上的所有缓存图像?

2 个答案:

答案 0 :(得分:2)

您只需将imagesource设置为NULL

即可

这样的事情:

BitmapImage bitmapImage = myimage.Source as BitmapImage;
bitmapImage.UriSource = null;
myimage.Source = null;

这对我有用。 Here你可以找到mor信息处理图像(例如图像缓存部分)。

答案 1 :(得分:0)

你回答这个问题有点晚了但你可以使用这个类来删除特定文件的缓存或者如果你想要的话删除所有文件

这是班助手

 class CacheCleanup : IDisposable
{
    private DispatcherTimer cleanCacheTimer;
    public CacheCleanup(TimeSpan? cleanInterval = null)
    {
        if (!cleanInterval.HasValue)
            cleanInterval = TimeSpan.FromMinutes(0.2);

        cleanCacheTimer = new DispatcherTimer();
        cleanCacheTimer.Interval = cleanInterval.Value;
        cleanCacheTimer.Tick += CleanCacheTimer_Tick;
        cleanCacheTimer.Start();
    }

    private void CleanCacheTimer_Tick(object sender, object e)
    {
        try
        {
            StorageFolder localDirectory = ApplicationData.Current.LocalFolder;
            string[] tmpCacheDirectories = Directory.GetDirectories(localDirectory.Path + "\\..\\ac\\inetcache");
            foreach (string dir in tmpCacheDirectories)
            {
                string[] tmpCacheFilesPng = Directory.GetFiles(dir, "*.png");
                foreach (string file in tmpCacheFilesPng)
                {

                        try
                        {
                            File.Delete(file);
                            Debug.WriteLine("Deleted png: " + file);
                        }
                        catch (Exception) { }
                }
                string[] tmpCacheFilesJpg = Directory.GetFiles(dir, "*.jpg");
                foreach (string file in tmpCacheFilesJpg)
                {

                    try
                    {
                        File.Delete(file);
                        Debug.WriteLine("Deleted jpg: " + file);
                    }
                    catch (Exception) { }
                }
            }
        }
        catch (Exception ex) { Debug.WriteLine("ERROR CLEANING CACHE: " + ex.Message); }
    }

    public void Dispose()
    {
        if (cleanCacheTimer != null)
        {
            cleanCacheTimer.Stop();
            cleanCacheTimer = null;
        }
    }
}

这就是你在c#代码的某些部分调用这个类的方法

CacheCleanup cacheCleanup = new CacheCleanup();
相关问题