2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2021-01-13 21:19:27 +01:00
|
|
|
|
// © 2021 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
using System;
|
2021-06-27 21:50:13 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#if WINUI
|
2021-06-27 21:50:13 +02:00
|
|
|
|
using Microsoft.System;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media.Imaging;
|
|
|
|
|
|
#else
|
2021-06-27 21:50:13 +02:00
|
|
|
|
using Windows.UI.Core;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
using Windows.UI.Xaml.Media.Imaging;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#endif
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2013-01-03 15:33:46 +01:00
|
|
|
|
public partial class Tile
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2021-06-27 21:50:13 +02:00
|
|
|
|
public async Task SetImageAsync(Func<Task<ImageSource>> loadImageFunc)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tcs = new TaskCompletionSource<object>();
|
|
|
|
|
|
|
|
|
|
|
|
async void callback()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
SetImage(await loadImageFunc());
|
|
|
|
|
|
tcs.SetResult(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.SetException(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#if WINUI
|
|
|
|
|
|
if (!Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, callback))
|
|
|
|
|
|
{
|
|
|
|
|
|
// should never happen, but just in case: reset Pending state and complete TaskCompletionSource
|
|
|
|
|
|
Pending = true;
|
|
|
|
|
|
tcs.SetResult(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
_ = Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, callback);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
_ = await tcs.Task.ConfigureAwait(false); // wait until image loading in UI thread is completed
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
public void SetImage(ImageSource image, bool fadeIn = true)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2015-01-20 17:52:02 +01:00
|
|
|
|
Pending = false;
|
|
|
|
|
|
|
2020-09-25 15:02:41 +02:00
|
|
|
|
if (image != null && fadeIn && MapBase.ImageFadeDuration > TimeSpan.Zero)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2020-04-16 23:15:03 +02:00
|
|
|
|
if (image is BitmapImage bitmap && bitmap.UriSource != null)
|
2017-07-17 21:31:09 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
bitmap.ImageOpened += BitmapImageOpened;
|
|
|
|
|
|
bitmap.ImageFailed += BitmapImageFailed;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-08-15 23:29:03 +02:00
|
|
|
|
FadeIn();
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-17 21:31:09 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Image.Opacity = 1d;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
Image.Source = image;
|
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
|
|
|
|
|
2018-08-15 23:29:03 +02:00
|
|
|
|
FadeIn();
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|