2025-09-10 20:09:12 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#if UWP
|
|
|
|
|
|
using Windows.UI.Core;
|
2021-07-02 21:18:39 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
2025-11-13 15:32:01 +01:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2025-09-10 20:09:12 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2024-05-19 17:24:18 +02:00
|
|
|
|
using Windows.UI.Xaml.Media.Animation;
|
2021-07-02 21:18:39 +02:00
|
|
|
|
using Windows.UI.Xaml.Media.Imaging;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#else
|
2025-09-10 20:09:12 +02:00
|
|
|
|
using Microsoft.UI.Dispatching;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
using Microsoft.UI.Xaml;
|
2025-11-13 15:32:01 +01:00
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
2025-09-10 20:09:12 +02:00
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
using Microsoft.UI.Xaml.Media.Animation;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media.Imaging;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#endif
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2025-11-13 15:32:01 +01:00
|
|
|
|
public class ImageTile(int zoomLevel, int x, int y, int columnCount)
|
|
|
|
|
|
: Tile(zoomLevel, x, y, columnCount)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2025-11-20 09:16:52 +01:00
|
|
|
|
public Image Image { get; } = new Image { Stretch = Stretch.Fill };
|
2025-11-13 15:32:01 +01:00
|
|
|
|
|
|
|
|
|
|
public override async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
|
2025-09-10 20:09:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
var tcs = new TaskCompletionSource<object>();
|
|
|
|
|
|
|
2025-09-10 22:19:20 +02:00
|
|
|
|
async void LoadAndSetImageSource()
|
2025-09-10 20:09:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var image = await loadImageFunc();
|
|
|
|
|
|
|
|
|
|
|
|
Image.Source = image;
|
|
|
|
|
|
|
|
|
|
|
|
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (image is BitmapImage bitmap && bitmap.UriSource != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
bitmap.ImageOpened += BitmapImageOpened;
|
|
|
|
|
|
bitmap.ImageFailed += BitmapImageFailed;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
BeginFadeInAnimation();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tcs.TrySetResult(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.TrySetException(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#if UWP
|
2025-09-10 22:19:20 +02:00
|
|
|
|
if (!await Image.Dispatcher.TryRunAsync(CoreDispatcherPriority.Low, LoadAndSetImageSource))
|
2025-09-10 20:09:12 +02:00
|
|
|
|
#else
|
2025-09-10 22:19:20 +02:00
|
|
|
|
if (!Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, LoadAndSetImageSource))
|
2025-09-10 20:09:12 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.TrySetCanceled();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await tcs.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
2025-01-05 09:22:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
From = 0d,
|
|
|
|
|
|
Duration = MapBase.ImageFadeDuration,
|
|
|
|
|
|
FillBehavior = FillBehavior.Stop
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-01-05 10:31:15 +01:00
|
|
|
|
Storyboard.SetTarget(fadeInAnimation, Image);
|
2025-01-25 16:47:42 +01:00
|
|
|
|
Storyboard.SetTargetProperty(fadeInAnimation, nameof(UIElement.Opacity));
|
2025-01-05 09:22:50 +01:00
|
|
|
|
|
|
|
|
|
|
var storyboard = new Storyboard();
|
2025-01-05 10:31:15 +01:00
|
|
|
|
storyboard.Children.Add(fadeInAnimation);
|
2025-01-05 09:22:50 +01:00
|
|
|
|
storyboard.Begin();
|
2024-05-19 17:24:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
private void BitmapImageOpened(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var bitmap = (BitmapImage)sender;
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
bitmap.ImageOpened -= BitmapImageOpened;
|
|
|
|
|
|
bitmap.ImageFailed -= BitmapImageFailed;
|
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 BitmapImageFailed(object sender, ExceptionRoutedEventArgs e)
|
|
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var bitmap = (BitmapImage)sender;
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
bitmap.ImageOpened -= BitmapImageOpened;
|
|
|
|
|
|
bitmap.ImageFailed -= BitmapImageFailed;
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
2013-01-03 15:33:46 +01:00
|
|
|
|
Image.Source = null;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|