mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 22:46:58 +00:00
Moved asynchronous image loading to Tile class
This commit is contained in:
parent
2f9c50fb47
commit
f4d43eeb44
10 changed files with 114 additions and 184 deletions
|
|
@ -1,9 +1,15 @@
|
|||
#if UWP
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
#if UWP
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Animation;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
#else
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.UI.Xaml.Media.Animation;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
#endif
|
||||
|
|
@ -12,6 +18,50 @@ namespace MapControl
|
|||
{
|
||||
public partial class Tile
|
||||
{
|
||||
public async Task LoadImageAsync(Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
|
||||
async void LoadImage()
|
||||
{
|
||||
try
|
||||
{
|
||||
var image = await loadImageFunc();
|
||||
|
||||
Image.Source = image;
|
||||
|
||||
if (image != null && MapBase.ImageFadeDuration > TimeSpan.Zero)
|
||||
{
|
||||
if (image is BitmapImage bitmap && bitmap.UriSource != null)
|
||||
{
|
||||
bitmap.ImageOpened += BitmapImageOpened;
|
||||
bitmap.ImageFailed += BitmapImageFailed;
|
||||
}
|
||||
else
|
||||
{
|
||||
BeginFadeInAnimation();
|
||||
}
|
||||
}
|
||||
|
||||
tcs.TrySetResult(null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
#if UWP
|
||||
if (!await Image.Dispatcher.TryRunAsync(CoreDispatcherPriority.Low, LoadImage))
|
||||
#else
|
||||
if (!Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, LoadImage))
|
||||
#endif
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
|
||||
await tcs.Task;
|
||||
}
|
||||
|
||||
private void BeginFadeInAnimation()
|
||||
{
|
||||
var fadeInAnimation = new DoubleAnimation
|
||||
|
|
@ -29,19 +79,6 @@ namespace MapControl
|
|||
storyboard.Begin();
|
||||
}
|
||||
|
||||
private void FadeIn()
|
||||
{
|
||||
if (Image.Source is BitmapImage bitmap && bitmap.UriSource != null)
|
||||
{
|
||||
bitmap.ImageOpened += BitmapImageOpened;
|
||||
bitmap.ImageFailed += BitmapImageFailed;
|
||||
}
|
||||
else
|
||||
{
|
||||
BeginFadeInAnimation();
|
||||
}
|
||||
}
|
||||
|
||||
private void BitmapImageOpened(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var bitmap = (BitmapImage)sender;
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
private static Task LoadTileImage(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource();
|
||||
|
||||
async void LoadTileImage()
|
||||
{
|
||||
try
|
||||
{
|
||||
tile.SetImageSource(await loadImageFunc());
|
||||
tcs.TrySetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
if (!tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, LoadTileImage))
|
||||
{
|
||||
tcs.TrySetCanceled();
|
||||
}
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue