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

78 lines
2.1 KiB
C#
Raw Normal View History

2025-12-05 15:07:44 +01:00
using System;
using System.Threading.Tasks;
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 DrawingTile : Tile
2025-12-05 15:07:44 +01:00
{
2026-04-13 17:14:49 +02:00
public DrawingTile(int zoomLevel, int x, int y, int columnCount)
: base(zoomLevel, x, y, columnCount)
2025-12-05 15:07:44 +01:00
{
2026-04-13 17:14:49 +02:00
Drawing.Children.Add(ImageDrawing);
}
public DrawingGroup Drawing { get; } = new DrawingGroup();
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
public ImageDrawing ImageDrawing { get; } = new ImageDrawing();
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
public override async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
{
var image = await loadImageFunc().ConfigureAwait(false);
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
void SetImageSource()
2025-12-05 15:07:44 +01:00
{
2026-04-13 17:14:49 +02:00
ImageDrawing.ImageSource = image;
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
2025-12-05 15:07:44 +01: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-12-05 15:07:44 +01:00
{
2026-04-13 17:14:49 +02:00
BeginFadeInAnimation();
2025-12-05 15:07:44 +01:00
}
}
}
2026-04-13 17:14:49 +02:00
await Drawing.Dispatcher.InvokeAsync(SetImageSource);
}
private void BeginFadeInAnimation()
{
var fadeInAnimation = new DoubleAnimation
2025-12-05 15:07:44 +01:00
{
2026-04-13 17:14:49 +02:00
From = 0d,
Duration = MapBase.ImageFadeDuration,
FillBehavior = FillBehavior.Stop
};
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
Drawing.BeginAnimation(DrawingGroup.OpacityProperty, fadeInAnimation);
}
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
private void BitmapDownloadCompleted(object sender, EventArgs e)
{
var bitmap = (BitmapSource)sender;
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed;
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
BeginFadeInAnimation();
}
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
{
var bitmap = (BitmapSource)sender;
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
bitmap.DownloadFailed -= BitmapDownloadFailed;
2025-12-05 15:07:44 +01:00
2026-04-13 17:14:49 +02:00
ImageDrawing.ImageSource = null;
2025-12-05 15:07:44 +01:00
}
}