mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-06 06:55:04 +00:00
Abstract classes Tile, TileSource
This commit is contained in:
parent
20e4fcce75
commit
cb8fff0dd1
14 changed files with 95 additions and 98 deletions
73
MapControl/WPF/ImageTile.WPF.cs
Normal file
73
MapControl/WPF/ImageTile.WPF.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class ImageTile(int zoomLevel, int x, int y, int columnCount)
|
||||
: Tile(zoomLevel, x, y, columnCount)
|
||||
{
|
||||
public Image Image { get; } = new Image { Stretch = Stretch.Fill };
|
||||
|
||||
public override async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var image = await loadImageFunc().ConfigureAwait(false);
|
||||
|
||||
void SetImageSource()
|
||||
{
|
||||
Image.Source = image;
|
||||
|
||||
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
|
||||
{
|
||||
if (image is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading)
|
||||
{
|
||||
bitmap.DownloadCompleted += BitmapDownloadCompleted;
|
||||
bitmap.DownloadFailed += BitmapDownloadFailed;
|
||||
}
|
||||
else
|
||||
{
|
||||
BeginFadeInAnimation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Image.Dispatcher.InvokeAsync(SetImageSource);
|
||||
}
|
||||
|
||||
private void BeginFadeInAnimation()
|
||||
{
|
||||
var fadeInAnimation = new DoubleAnimation
|
||||
{
|
||||
From = 0d,
|
||||
Duration = MapBase.ImageFadeDuration,
|
||||
FillBehavior = FillBehavior.Stop
|
||||
};
|
||||
|
||||
Image.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
|
||||
}
|
||||
|
||||
private void BitmapDownloadCompleted(object sender, EventArgs e)
|
||||
{
|
||||
var bitmap = (BitmapSource)sender;
|
||||
|
||||
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
|
||||
bitmap.DownloadFailed -= BitmapDownloadFailed;
|
||||
|
||||
BeginFadeInAnimation();
|
||||
}
|
||||
|
||||
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)
|
||||
{
|
||||
var bitmap = (BitmapSource)sender;
|
||||
|
||||
bitmap.DownloadCompleted -= BitmapDownloadCompleted;
|
||||
bitmap.DownloadFailed -= BitmapDownloadFailed;
|
||||
|
||||
Image.Source = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue