2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2025-09-10 20:09:12 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2024-05-19 17:24:18 +02:00
|
|
|
|
using System.Windows;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
using System.Windows.Controls;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Windows.Media;
|
2024-05-19 17:24:18 +02:00
|
|
|
|
using System.Windows.Media.Animation;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2013-01-03 15:33:46 +01:00
|
|
|
|
public partial class Tile
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2025-09-10 20:09:12 +02:00
|
|
|
|
public async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
|
|
|
|
|
|
{
|
|
|
|
|
|
var image = await loadImageFunc().ConfigureAwait(false);
|
|
|
|
|
|
|
2025-09-10 21:14:18 +02:00
|
|
|
|
void SetImageSource()
|
|
|
|
|
|
{
|
|
|
|
|
|
Image.Source = image;
|
2025-09-10 20:09:12 +02:00
|
|
|
|
|
2025-09-10 21:14:18 +02:00
|
|
|
|
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (image is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading)
|
2025-09-10 20:09:12 +02:00
|
|
|
|
{
|
2025-09-10 21:14:18 +02:00
|
|
|
|
bitmap.DownloadCompleted += BitmapDownloadCompleted;
|
|
|
|
|
|
bitmap.DownloadFailed += BitmapDownloadFailed;
|
2025-09-10 20:09:12 +02:00
|
|
|
|
}
|
2025-09-10 21:14:18 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
BeginFadeInAnimation();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await Image.Dispatcher.InvokeAsync(SetImageSource);
|
2025-09-10 20:09:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-05 10:31:15 +01:00
|
|
|
|
private void BeginFadeInAnimation()
|
2024-05-19 17:24:18 +02:00
|
|
|
|
{
|
2025-01-05 10:31:15 +01:00
|
|
|
|
var fadeInAnimation = new DoubleAnimation
|
|
|
|
|
|
{
|
|
|
|
|
|
From = 0d,
|
|
|
|
|
|
Duration = MapBase.ImageFadeDuration,
|
|
|
|
|
|
FillBehavior = FillBehavior.Stop
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Image.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
|
2024-05-19 17:24:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
private void BitmapDownloadCompleted(object sender, EventArgs e)
|
|
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var bitmap = (BitmapSource)sender;
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
|
|
|
|
|
|
bitmap.DownloadFailed -= BitmapDownloadFailed;
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
2025-01-05 10:31:15 +01:00
|
|
|
|
BeginFadeInAnimation();
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
|
|
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var bitmap = (BitmapSource)sender;
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
|
|
|
|
|
|
bitmap.DownloadFailed -= BitmapDownloadFailed;
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
Image.Source = null;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|