2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2025-08-20 16:44:48 +02:00
|
|
|
|
using System.Threading;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2021-06-28 23:35:03 +02:00
|
|
|
|
using Windows.UI.Core;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public partial class TileImageLoader
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2025-08-20 16:44:48 +02:00
|
|
|
|
private static async Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc, CancellationToken cancellationToken)
|
2021-06-28 23:35:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
var tcs = new TaskCompletionSource<object>();
|
|
|
|
|
|
|
2025-01-16 19:57:00 +01:00
|
|
|
|
async void LoadTileImage()
|
2021-06-28 23:35:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-01-16 19:57:00 +01:00
|
|
|
|
var image = await loadImageFunc();
|
2025-01-21 19:28:49 +01:00
|
|
|
|
|
|
|
|
|
|
tcs.TrySetResult(null); // tcs.Task has completed when image is loaded
|
2025-01-16 19:57:00 +01:00
|
|
|
|
|
2025-08-20 16:44:48 +02:00
|
|
|
|
if (!cancellationToken.IsCancellationRequested)
|
|
|
|
|
|
{
|
|
|
|
|
|
tile.SetImageSource(image);
|
|
|
|
|
|
}
|
2021-06-28 23:35:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.TrySetException(ex);
|
|
|
|
|
|
}
|
2021-07-05 00:03:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-21 19:28:49 +01:00
|
|
|
|
if (!await tile.Image.Dispatcher.TryRunAsync(CoreDispatcherPriority.Low, LoadTileImage))
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.TrySetCanceled();
|
|
|
|
|
|
}
|
2021-06-28 23:35:03 +02:00
|
|
|
|
|
2025-01-21 19:28:49 +01:00
|
|
|
|
await tcs.Task;
|
2021-06-28 23:35:03 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|