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

43 lines
1.1 KiB
C#

using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Media;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MapControl
{
public partial class TileImageLoader
{
private static Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource();
async void LoadTileImage()
{
try
{
var image = await loadImageFunc();
tcs.TrySetResult(); // tcs.Task has completed when image is loaded
if (!cancellationToken.IsCancellationRequested)
{
tile.SetImageSource(image);
}
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
if (!tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, LoadTileImage))
{
tcs.TrySetCanceled();
}
return tcs.Task;
}
}
}