XAML-Map-Control/MapControl/UWP/TileImageLoader.UWP.cs
2025-08-20 19:50:22 +02:00

47 lines
1.3 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Core;
using Windows.UI.Xaml.Media;
namespace MapControl
{
public partial class TileImageLoader
{
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;
tcs.TrySetCanceled(cancellationToken);
}
else
{
tile.SetImageSource(image);
tcs.TrySetResult(null);
}
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
if (!await tile.Image.Dispatcher.TryRunAsync(CoreDispatcherPriority.Low, LoadTileImage))
{
tcs.TrySetCanceled(CancellationToken.None);
}
await tcs.Task;
}
}
}