private void RaiseTileLoad(Tile tile, Rectangle2D bounds)
        {
            if (TileLoaded != null)
            {
                TileLoadEventArgs loadTileArgs = new TileLoadEventArgs();
                loadTileArgs.Column = tile.Column;
                loadTileArgs.ImageSource = tile.Image.Source;

                loadTileArgs.Level = tile.Level;
                loadTileArgs.Row = tile.Row;
                loadTileArgs.Bounds = bounds;

                TileLoaded(this, loadTileArgs);
            }
        }
        private void AddTiles(double resolution, int startCol, int startRow, int endCol, int endRow, bool useTransitions)
        {
            double num = ((endRow - startRow) * 0.5) + startRow;
            double num2 = ((endCol - startCol) * 0.5) + startCol;
            List<DistanceTile> list = new List<DistanceTile>();
            for (int i = startRow; i <= endRow; i++)
            {
                for (int j = startCol; j <= endCol; j++)
                {
                    string str = Tile.GetTileKey(i, j, resolution, realResolutions);
                    Image image = base.Container.FindName(uniqueLayerId + str) as Image;
                    if (image == null)
                    {
                        list.Add(new DistanceTile { Row = i, Column = j, Distance = Math.Pow(i - num, 2.0) + Math.Pow(j - num2, 2.0) });
                    }
                    else
                    {
                        image.ClearValue(LayerContainer.OutdatedProperty);
                    }//如果find,说明是上次的,可以标记outdated,予以清除
                }
            }
            list.Sort();//排序?按照distance删除重复的?
            int level = MathUtil.GetIndex(resolution, realResolutions);
            foreach (DistanceTile disTile in list)
            {
                string url = string.Empty;
                if (this.GetCachedResolutions() != null)
                {
                    if (level == -1)
                    {
                        return;//CachedLayer中没有Map的某个尺度
                    }
                    url = this.GetTileUrl(disTile.Column, disTile.Row, level, double.NaN);
                }
                else
                {
                    url = this.GetTileUrl(disTile.Column, disTile.Row, int.MinValue, resolution);
                    //统一管理token
            #if SILVERLIGHT
                    if (Credential.CREDENTIAL.GetUrlParameters() != null)
                    {
                        if (url.IndexOf("?") > 0)
                            url += "&" + Credential.CREDENTIAL.GetUrlParameters();
                        else
                            url += "?" + Credential.CREDENTIAL.GetUrlParameters();
                    }
            #endif
                }

                Tile tile = new Tile(disTile.Row, disTile.Column, resolution, realResolutions, url, useTransitions);

                if (TileLoading != null)
                {
                    double left = this.Origin.X + ((TileSize * tile.Column) * resolution);
                    double top = this.Origin.Y - ((TileSize * tile.Row) * resolution);
                    double right = left + (this.TileSize * resolution);
                    double bottom = top - (this.TileSize * resolution);
                    TileLoadEventArgs loadingArgs = new TileLoadEventArgs
                    {
                        Column = tile.Column,
                        Level = tile.Level,
                        Row = tile.Row,
                        Bounds = new Rectangle2D(left, bottom, right, top),
                    };
                    TileLoading(this, loadingArgs);
                }
                this.AddTile(tile);
            }
        }
 private void bmi_DownloadProgress(object sender, DownloadProgressEventArgs e, Tile tile, EventHandler<DownloadProgressEventArgs> onProgressEventHandler)
 {
     tile.Progress = e.Progress;
     if (!string.IsNullOrEmpty(tile.Url) && (tile.Image != null))
     {
         if (e.Progress == 100)
         {
             (sender as BitmapImage).DownloadProgress -= onProgressEventHandler;
             if (!this.pendingTiles.ContainsKey(tile.Url))
             {
                 if (base.Container != null)
                 {
                     base.Container.Children.Remove(tile.Image);
                 }
                 return;
             }
             bool useTransitions = tile.UseTransitions;
             ShowImage(tile.Image, useTransitions);
             pendingTiles.Remove(tile.Url);
         }
         base.OnProgress(GetProgress());
     }
 }
        private void AddTile(Tile tile)
        {
            if (!string.IsNullOrEmpty(tile.Url) && !pendingTiles.ContainsKey(tile.Url))
            {
                BitmapImage bitmapImage = new BitmapImage()
                {
                    UriSource = new Uri(tile.Url, UriKind.Absolute)
                };

                EventHandler<DownloadProgressEventArgs> onProgressEventHandler = null;
                onProgressEventHandler = delegate(object sender, DownloadProgressEventArgs e)
                {
                    this.bmi_DownloadProgress(sender, e, tile, onProgressEventHandler);
                };
                bitmapImage.DownloadProgress += onProgressEventHandler;

                tile.Image = new Image
                {
                    Opacity = 0.0,
                    Tag = tile,
                    IsHitTestVisible = false,
                    Name = uniqueLayerId + tile.TileKey,
                    Stretch = Stretch.Fill,
                    Source = bitmapImage
                };
                tile.Image.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(this.img_ImageFailed);

                double resolution = tile.Resolution;
                //orginX originY
                double left = this.Origin.X + ((TileSize * tile.Column) * resolution);
                double top = this.Origin.Y - ((TileSize * tile.Row) * resolution);
                double right = left + (this.TileSize * resolution);
                double bottom = top - (this.TileSize * resolution);
                LayerContainer.SetBounds(tile.Image, new Rectangle2D(left, bottom, right, top));//计算该image的范围

                tile.Image.ImageOpened += delegate(object o, RoutedEventArgs e)
                {
                    if (base.Dispatcher.CheckAccess())
                    {
                        Action a = delegate
                        {
                            this.RaiseTileLoad(tile, new Rectangle2D(left, bottom, right, top));
                        };

                        this.Dispatcher.BeginInvoke(a);
                    }
                    else
                    {
                        this.RaiseTileLoad(tile, new Rectangle2D(left, bottom, right, top));
                    }
                };
                pendingTiles.Add(tile.Url, tile);
                base.Container.Children.Add(tile.Image);
            }
        }