Moved asynchronous image loading to Tile class

This commit is contained in:
ClemensFischer 2025-09-10 20:09:12 +02:00
parent 2f9c50fb47
commit f4d43eeb44
10 changed files with 114 additions and 184 deletions

View file

@ -1,33 +1,47 @@
using Avalonia;
using Avalonia.Animation;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.Threading;
using System;
using System.Threading.Tasks;
namespace MapControl
{
public partial class Tile
{
private void FadeIn()
public async Task LoadImageAsync(Func<Task<IImage>> loadImageFunc)
{
var fadeInAnimation = new Animation
{
Duration = MapBase.ImageFadeDuration,
Children =
{
new KeyFrame
{
KeyTime = TimeSpan.Zero,
Setters = { new Setter(Visual.OpacityProperty, 0d) }
},
new KeyFrame
{
KeyTime = MapBase.ImageFadeDuration,
Setters = { new Setter(Visual.OpacityProperty, 1d) }
}
}
};
var image = await loadImageFunc().ConfigureAwait(false);
_ = fadeInAnimation.RunAsync(Image);
await Dispatcher.UIThread.InvokeAsync(
() =>
{
Image.Source = image;
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
{
var fadeInAnimation = new Animation
{
Duration = MapBase.ImageFadeDuration,
Children =
{
new KeyFrame
{
KeyTime = TimeSpan.Zero,
Setters = { new Setter(Visual.OpacityProperty, 0d) }
},
new KeyFrame
{
KeyTime = MapBase.ImageFadeDuration,
Setters = { new Setter(Visual.OpacityProperty, 1d) }
}
}
};
_ = fadeInAnimation.RunAsync(Image);
}
});
}
}
}

View file

@ -1,18 +0,0 @@
using Avalonia.Media;
using Avalonia.Threading;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MapControl
{
public partial class TileImageLoader
{
private static async Task LoadTileImage(Tile tile, Func<Task<IImage>> loadImageFunc)
{
var image = await loadImageFunc().ConfigureAwait(false);
await Dispatcher.UIThread.InvokeAsync(() => tile.SetImageSource(image));
}
}
}