XAML-Map-Control/MapControl/UWP/TileImageLoader.UWP.cs

47 lines
1.3 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
using System.Threading;
2017-08-04 21:38:58 +02:00
using System.Threading.Tasks;
using Windows.UI.Core;
using Windows.UI.Xaml.Media;
2017-08-04 21:38:58 +02:00
namespace MapControl
{
public partial class TileImageLoader
2017-08-04 21:38:58 +02:00
{
private static async Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<object>();
async void LoadTileImage()
{
try
{
var image = await loadImageFunc();
if (cancellationToken.IsCancellationRequested)
{
tile.IsPending = true;
2025-08-20 19:50:22 +02:00
tcs.TrySetCanceled(cancellationToken);
}
else
{
tile.SetImageSource(image);
2025-08-20 19:50:22 +02:00
tcs.TrySetResult(null);
}
}
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))
{
2025-08-20 19:50:22 +02:00
tcs.TrySetCanceled(CancellationToken.None);
2025-01-21 19:28:49 +01:00
}
2025-01-21 19:28:49 +01:00
await tcs.Task;
}
2017-08-04 21:38:58 +02:00
}
}