// XAML Map Control - http://xamlmapcontrol.codeplex.com/ // Copyright © 2014 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; namespace MapControl { public partial class Tile { public void SetImageSource(ImageSource image, bool animateOpacity) { if (image != null && Image.Source == null) { if (animateOpacity) { var bitmap = image as BitmapSource; if (bitmap != null && !bitmap.IsFrozen && bitmap.IsDownloading) { bitmap.DownloadCompleted += BitmapDownloadCompleted; bitmap.DownloadFailed += BitmapDownloadFailed; } else { Image.BeginAnimation(Image.OpacityProperty, new DoubleAnimation(1d, Settings.TileAnimationDuration)); } } else { Image.Opacity = 1d; } } Image.Source = image; HasImageSource = true; } private void BitmapDownloadCompleted(object sender, EventArgs e) { var bitmap = (BitmapSource)sender; bitmap.DownloadCompleted -= BitmapDownloadCompleted; bitmap.DownloadFailed -= BitmapDownloadFailed; Image.BeginAnimation(Image.OpacityProperty, new DoubleAnimation(1d, Settings.TileAnimationDuration)); } private void BitmapDownloadFailed(object sender, ExceptionEventArgs e) { var bitmap = (BitmapSource)sender; bitmap.DownloadCompleted -= BitmapDownloadCompleted; bitmap.DownloadFailed -= BitmapDownloadFailed; Image.Source = null; } } }