TileImageLoader

This commit is contained in:
ClemensFischer 2025-01-21 19:28:49 +01:00
parent 115525cbfc
commit b3d147bf5e
4 changed files with 12 additions and 7 deletions

View file

@ -13,7 +13,7 @@ namespace MapControl
{ {
var image = await loadImageFunc().ConfigureAwait(false); var image = await loadImageFunc().ConfigureAwait(false);
await Dispatcher.UIThread.InvokeAsync(() => tile.SetImageSource(image)); _ = Dispatcher.UIThread.InvokeAsync(() => tile.SetImageSource(image)); // no need to await InvokeAsync
} }
} }
} }

View file

@ -11,7 +11,7 @@ namespace MapControl
{ {
public partial class TileImageLoader public partial class TileImageLoader
{ {
private static Task<object> LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc) private static async Task LoadTileAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
{ {
var tcs = new TaskCompletionSource<object>(); var tcs = new TaskCompletionSource<object>();
@ -20,9 +20,10 @@ namespace MapControl
try try
{ {
var image = await loadImageFunc(); var image = await loadImageFunc();
tcs.TrySetResult(null); // tcs.Task has completed when image is loaded
tile.SetImageSource(image); tile.SetImageSource(image);
tcs.TrySetResult(null);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -30,9 +31,12 @@ namespace MapControl
} }
} }
_ = tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, LoadTileImage); if (!await tile.Image.Dispatcher.TryRunAsync(CoreDispatcherPriority.Low, LoadTileImage))
{
tcs.TrySetCanceled();
}
return tcs.Task; await tcs.Task;
} }
} }
} }

View file

@ -14,7 +14,7 @@ namespace MapControl
{ {
var image = await loadImageFunc().ConfigureAwait(false); var image = await loadImageFunc().ConfigureAwait(false);
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImageSource(image)); _ = tile.Image.Dispatcher.InvokeAsync(() => tile.SetImageSource(image)); // no need to await InvokeAsync
} }
} }
} }

View file

@ -21,8 +21,9 @@ namespace MapControl
{ {
var image = await loadImageFunc(); var image = await loadImageFunc();
tcs.TrySetResult(); // tcs.Task has completed when image is loaded
tile.SetImageSource(image); tile.SetImageSource(image);
tcs.TrySetResult();
} }
catch (Exception ex) catch (Exception ex)
{ {