diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index 4c605f9e..c386467f 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -239,7 +239,7 @@ namespace MapControl if (equivalentTile != null) { - tile.SetImageSource(equivalentTile); + tile.SetImageSource(equivalentTile.Image.Source, false); } } diff --git a/MapControl/Shared/Tile.cs b/MapControl/Shared/Tile.cs index be9e028d..a022c7e8 100644 --- a/MapControl/Shared/Tile.cs +++ b/MapControl/Shared/Tile.cs @@ -2,6 +2,7 @@ // © 2022 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) +using System; #if WINUI using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -52,14 +53,22 @@ namespace MapControl public bool Pending { get; set; } = true; - public void SetImageSource(Tile tile) + public void SetImageSource(ImageSource image, bool animateOpacity = true) { Pending = false; - Image.Opacity = 1d; - Image.Source = tile.Image.Source; + Image.Source = image; + + if (image != null && animateOpacity && MapBase.ImageFadeDuration > TimeSpan.Zero) + { + AnimateImageOpacity(); + } + else + { + Image.Opacity = 1d; + } } - private void FadeIn() + private void BeginOpacityAnimation() { Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { diff --git a/MapControl/WPF/Tile.WPF.cs b/MapControl/WPF/Tile.WPF.cs index 4da777e1..afdb961e 100644 --- a/MapControl/WPF/Tile.WPF.cs +++ b/MapControl/WPF/Tile.WPF.cs @@ -11,28 +11,17 @@ namespace MapControl { public partial class Tile { - public void SetImageSource(ImageSource image) + private void AnimateImageOpacity() { - Pending = false; - - if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero) + if (Image.Source is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading) { - if (image is BitmapSource bitmap && !bitmap.IsFrozen && bitmap.IsDownloading) - { - bitmap.DownloadCompleted += BitmapDownloadCompleted; - bitmap.DownloadFailed += BitmapDownloadFailed; - } - else - { - FadeIn(); - } + bitmap.DownloadCompleted += BitmapDownloadCompleted; + bitmap.DownloadFailed += BitmapDownloadFailed; } else { - Image.Opacity = 1d; + BeginOpacityAnimation(); } - - Image.Source = image; } private void BitmapDownloadCompleted(object sender, EventArgs e) @@ -42,7 +31,7 @@ namespace MapControl bitmap.DownloadCompleted -= BitmapDownloadCompleted; bitmap.DownloadFailed -= BitmapDownloadFailed; - FadeIn(); + BeginOpacityAnimation(); } private void BitmapDownloadFailed(object sender, ExceptionEventArgs e) diff --git a/MapControl/WinUI/Tile.WinUI.cs b/MapControl/WinUI/Tile.WinUI.cs index 138311f3..862c27f4 100644 --- a/MapControl/WinUI/Tile.WinUI.cs +++ b/MapControl/WinUI/Tile.WinUI.cs @@ -2,14 +2,11 @@ // © 2022 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) -using System; #if WINUI using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media.Imaging; #else using Windows.UI.Xaml; -using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; #endif @@ -17,28 +14,17 @@ namespace MapControl { public partial class Tile { - public void SetImageSource(ImageSource image) + private void AnimateImageOpacity() { - Pending = false; - - if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero) + if (Image.Source is BitmapImage bitmap && bitmap.UriSource != null) { - if (image is BitmapImage bitmap && bitmap.UriSource != null) - { - bitmap.ImageOpened += BitmapImageOpened; - bitmap.ImageFailed += BitmapImageFailed; - } - else - { - FadeIn(); - } + bitmap.ImageOpened += BitmapImageOpened; + bitmap.ImageFailed += BitmapImageFailed; } else { - Image.Opacity = 1d; + BeginOpacityAnimation(); } - - Image.Source = image; } private void BitmapImageOpened(object sender, RoutedEventArgs e) @@ -48,7 +34,7 @@ namespace MapControl bitmap.ImageOpened -= BitmapImageOpened; bitmap.ImageFailed -= BitmapImageFailed; - FadeIn(); + BeginOpacityAnimation(); } private void BitmapImageFailed(object sender, ExceptionRoutedEventArgs e)