XAML-Map-Control/MapControl/Tile.WPF.cs
2015-01-20 17:52:02 +01:00

67 lines
2 KiB
C#

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// © 2015 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 SetImage(ImageSource image, bool animateOpacity = true)
{
Pending = false;
if (image != null)
{
if (animateOpacity && OpacityAnimationDuration > TimeSpan.Zero)
{
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(0d, 1d, OpacityAnimationDuration));
}
}
else
{
Image.Opacity = 1d;
}
Image.Source = image;
}
}
private void BitmapDownloadCompleted(object sender, EventArgs e)
{
var bitmap = (BitmapSource)sender;
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed;
Image.BeginAnimation(Image.OpacityProperty,
new DoubleAnimation(0d, 1d, OpacityAnimationDuration));
}
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
{
var bitmap = (BitmapSource)sender;
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed;
Image.Source = null;
}
}
}