XAML-Map-Control/MapControl/UWP/TileImageLoader.UWP.cs
2025-08-20 16:44:48 +02:00

43 lines
1.1 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();
tcs.TrySetResult(null); // tcs.Task has completed when image is loaded
if (!cancellationToken.IsCancellationRequested)
{
tile.SetImageSource(image);
}
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
if (!await tile.Image.Dispatcher.TryRunAsync(CoreDispatcherPriority.Low, LoadTileImage))
{
tcs.TrySetCanceled();
}
await tcs.Task;
}
}
}