mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Animation;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace MapControl
|
|
{
|
|
public class ImageTile(int zoomLevel, int x, int y, int columnCount)
|
|
: Tile(zoomLevel, x, y, columnCount)
|
|
{
|
|
public Image Image { get; } = new Image { Stretch = Stretch.Fill };
|
|
|
|
public override async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
|
|
{
|
|
var image = await loadImageFunc().ConfigureAwait(false);
|
|
|
|
void SetImageSource()
|
|
{
|
|
Image.Source = image;
|
|
|
|
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
|
|
{
|
|
if (image is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading)
|
|
{
|
|
bitmap.DownloadCompleted += BitmapDownloadCompleted;
|
|
bitmap.DownloadFailed += BitmapDownloadFailed;
|
|
}
|
|
else
|
|
{
|
|
BeginFadeInAnimation();
|
|
}
|
|
}
|
|
}
|
|
|
|
await Image.Dispatcher.InvokeAsync(SetImageSource);
|
|
}
|
|
|
|
private void BeginFadeInAnimation()
|
|
{
|
|
var fadeInAnimation = new DoubleAnimation
|
|
{
|
|
From = 0d,
|
|
Duration = MapBase.ImageFadeDuration,
|
|
FillBehavior = FillBehavior.Stop
|
|
};
|
|
|
|
Image.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
|
|
}
|
|
|
|
private void BitmapDownloadCompleted(object sender, EventArgs e)
|
|
{
|
|
var bitmap = (BitmapSource)sender;
|
|
|
|
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
|
|
bitmap.DownloadFailed -= BitmapDownloadFailed;
|
|
|
|
BeginFadeInAnimation();
|
|
}
|
|
|
|
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
|
|
{
|
|
var bitmap = (BitmapSource)sender;
|
|
|
|
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
|
|
bitmap.DownloadFailed -= BitmapDownloadFailed;
|
|
|
|
Image.Source = null;
|
|
}
|
|
}
|
|
}
|