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

43 lines
1.1 KiB
C#
Raw Normal View History

2021-07-02 21:18:39 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2025-01-01 18:57:55 +01:00
// Copyright © Clemens Fischer
2021-07-02 21:18:39 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
2021-07-05 00:03:44 +02:00
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Media;
2021-07-02 21:18:39 +02:00
using System;
using System.Threading.Tasks;
namespace MapControl
{
public partial class TileImageLoader
{
private static Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
2021-07-02 21:18:39 +02:00
{
var tcs = new TaskCompletionSource();
async void LoadTileImage()
2021-07-02 21:18:39 +02:00
{
try
{
var image = await loadImageFunc();
2025-01-21 19:28:49 +01:00
tcs.TrySetResult(); // tcs.Task has completed when image is loaded
tile.SetImageSource(image);
2021-07-02 21:18:39 +02:00
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
2021-07-05 00:03:44 +02:00
}
if (!tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, LoadTileImage))
{
tcs.TrySetCanceled();
}
2021-07-02 21:18:39 +02:00
return tcs.Task;
}
}
}