XAML-Map-Control/MapControl/WPF/Tile.WPF.cs

65 lines
2 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace MapControl
{
public partial class Tile
{
2017-08-04 21:38:58 +02:00
public void SetImage(ImageSource imageSource, bool fadeIn = true)
{
2015-01-20 17:52:02 +01:00
Pending = false;
2017-07-17 21:31:09 +02:00
if (fadeIn && FadeDuration > TimeSpan.Zero)
{
2017-08-04 21:38:58 +02:00
var bitmapSource = imageSource as BitmapSource;
2017-07-17 21:31:09 +02:00
2017-08-04 21:38:58 +02:00
if (bitmapSource != null && !bitmapSource.IsFrozen && bitmapSource.IsDownloading)
2017-07-17 21:31:09 +02:00
{
2017-08-04 21:38:58 +02:00
bitmapSource.DownloadCompleted += BitmapDownloadCompleted;
bitmapSource.DownloadFailed += BitmapDownloadFailed;
}
else
{
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation(0d, 1d, FadeDuration, FillBehavior.Stop));
Image.Opacity = 1d;
}
}
2017-07-17 21:31:09 +02:00
else
{
Image.Opacity = 1d;
}
2017-08-04 21:38:58 +02:00
Image.Source = imageSource;
}
private void BitmapDownloadCompleted(object sender, EventArgs e)
{
2017-08-04 21:38:58 +02:00
var bitmapSource = (BitmapSource)sender;
2017-08-04 21:38:58 +02:00
bitmapSource.DownloadCompleted -= BitmapDownloadCompleted;
bitmapSource.DownloadFailed -= BitmapDownloadFailed;
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation(0d, 1d, FadeDuration, FillBehavior.Stop));
Image.Opacity = 1d;
}
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
{
2017-08-04 21:38:58 +02:00
var bitmapSource = (BitmapSource)sender;
2017-08-04 21:38:58 +02:00
bitmapSource.DownloadCompleted -= BitmapDownloadCompleted;
bitmapSource.DownloadFailed -= BitmapDownloadFailed;
Image.Source = null;
}
}
}