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

73 lines
2 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
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;
2026-04-13 17:14:49 +02:00
namespace MapControl;
public class ImageTile(int zoomLevel, int x, int y, int columnCount)
: Tile(zoomLevel, x, y, columnCount)
{
2026-04-13 17:14:49 +02:00
public Image Image { get; } = new Image { Stretch = Stretch.Fill };
public override async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
{
2026-04-13 17:14:49 +02:00
var image = await loadImageFunc().ConfigureAwait(false);
2025-11-13 15:32:01 +01:00
2026-04-13 17:14:49 +02:00
void SetImageSource()
{
2026-04-13 17:14:49 +02:00
Image.Source = image;
2026-04-13 17:14:49 +02:00
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
2025-09-10 21:14:18 +02:00
{
2026-04-13 17:14:49 +02:00
if (image is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading)
{
bitmap.DownloadCompleted += BitmapDownloadCompleted;
bitmap.DownloadFailed += BitmapDownloadFailed;
}
else
2025-09-10 21:14:18 +02:00
{
2026-04-13 17:14:49 +02:00
BeginFadeInAnimation();
2025-09-10 21:14:18 +02:00
}
}
}
2026-04-13 17:14:49 +02:00
await Image.Dispatcher.InvokeAsync(SetImageSource);
}
private void BeginFadeInAnimation()
{
var fadeInAnimation = new DoubleAnimation
{
2026-04-13 17:14:49 +02:00
From = 0d,
Duration = MapBase.ImageFadeDuration,
FillBehavior = FillBehavior.Stop
};
2025-01-05 10:31:15 +01:00
2026-04-13 17:14:49 +02:00
Image.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
}
2026-04-13 17:14:49 +02:00
private void BitmapDownloadCompleted(object sender, EventArgs e)
{
var bitmap = (BitmapSource)sender;
2026-04-13 17:14:49 +02:00
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed;
2026-04-13 17:14:49 +02:00
BeginFadeInAnimation();
}
2026-04-13 17:14:49 +02:00
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
{
var bitmap = (BitmapSource)sender;
2026-04-13 17:14:49 +02:00
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed;
2026-04-13 17:14:49 +02:00
Image.Source = null;
}
}