public static void RefreshCache(this TileOverlay overlay, bool enabled, string cacheId, string cacheDirectory)
        {
            BitmapTileCache tempTileCache = overlay.TileCache as BitmapTileCache;

            FileBitmapTileCache newCache = null;

            if (enabled)
            {
                newCache = new FileBitmapTileCache(cacheDirectory, cacheId);
                if (overlay.MapArguments != null)
                {
                    newCache.TileMatrix.BoundingBoxUnit = overlay.MapArguments.MapUnit;
                }
            }

            if (newCache != null)
            {
                newCache.TileMatrix.TileHeight = overlay.TileHeight;
                newCache.TileMatrix.TileWidth  = overlay.TileWidth;
            }

            overlay.TileCache = newCache;
            if (!overlay.IsBase && tempTileCache != null)
            {
                Task.Factory.StartNew(cache =>
                {
                    BitmapTileCache removingCache = (BitmapTileCache)cache;
                    lock (removingCache)
                    {
                        try { removingCache.ClearCache(); }
                        catch { }
                    }
                }, tempTileCache);
            }
        }
        public static void ClearCaches(this TileOverlay overlay)
        {
            BitmapTileCache tileCache = overlay.TileCache;

            if (tileCache != null)
            {
                lock (tileCache)
                {
                    try
                    {
                        tileCache.ClearCache();
                    }
                    catch { }
                }
            }
        }
        public static void ClearCaches(this TileOverlay overlay, RectangleShape extent)
        {
            BitmapTileCache tileCache = overlay.TileCache;

            if (tileCache != null)
            {
                lock (tileCache)
                {
                    try
                    {
                        tileCache.DeleteTiles(extent);
                    }
                    catch { }
                }
            }
        }
        public static void RefreshCache(this TileOverlay overlay, bool enabled)
        {
            BingMapsOverlay       bingOverlay = overlay as BingMapsOverlay;
            WorldMapKitMapOverlay wmkOverlay  = overlay as WorldMapKitMapOverlay;
            OpenStreetMapOverlay  osmOverlay  = overlay as OpenStreetMapOverlay;

            string cacheId                = string.Empty;
            string cacheFolder            = string.Empty;
            bool   needRefresh            = overlay.TileCache == null;
            FileBitmapTileCache tileCache = null;

            if (bingOverlay != null)
            {
                cacheId               = bingOverlay.MapType.ToString();
                cacheFolder           = Path.Combine(TemporaryPath, "BingMap");
                bingOverlay.TileCache = null;
                needRefresh           = true;

                if (enabled)
                {
                    tileCache = GetTileCache(overlay, cacheFolder, cacheId);
                }
                bingOverlay.TileCache = tileCache;
                overlay.TileCache     = tileCache;
            }
            else if (wmkOverlay != null)
            {
                //cacheId = wmkOverlay.Projection.ToString();
                cacheId = GetDefaultCacheId(wmkOverlay.LayerType, wmkOverlay.Projection, wmkOverlay.MapType);
                string layerType = wmkOverlay.LayerType.ToString();
                if (layerType == Layers.WorldMapKitLayerType.Default.ToString())
                {
                    layerType = Layers.WorldMapKitLayerType.OSMWorldMapKitLayer.ToString();
                }
                cacheFolder = Path.Combine(TemporaryPath, layerType);
                needRefresh = true;

                if (enabled)
                {
                    tileCache = GetTileCache(overlay, cacheFolder, cacheId);
                }
                overlay.TileCache = tileCache;
            }
            else if (osmOverlay != null)
            {
                cacheId              = "SphereMercator";
                cacheFolder          = Path.Combine(TemporaryPath, "OpenStreetMap");
                osmOverlay.TileCache = null;

                if (enabled)
                {
                    tileCache = GetTileCache(overlay, cacheFolder, cacheId);
                }
                osmOverlay.TileCache = tileCache;
                overlay.TileCache    = tileCache;
            }
            else
            {
                cacheId     = Guid.NewGuid().ToString();
                cacheFolder = TemporaryPath;
                if (enabled)
                {
                    tileCache = GetTileCache(overlay, cacheFolder, cacheId);
                }
                overlay.TileCache = tileCache;
                needRefresh       = true;
            }

            if (needRefresh)
            {
                BitmapTileCache tempTileCache = overlay.TileCache as BitmapTileCache;
                if (!overlay.IsBase && tempTileCache != null)
                {
                    Task.Factory.StartNew(cache =>
                    {
                        BitmapTileCache removingCache = (BitmapTileCache)cache;
                        lock (removingCache)
                        {
                            try { removingCache.ClearCache(); }
                            catch { }
                        }
                    }, tempTileCache);
                }
            }
        }