mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Updated TileImageLoader and ImageFileCache for WinUI
This commit is contained in:
parent
9ebc88204b
commit
a9475f79fe
7 changed files with 90 additions and 75 deletions
|
|
@ -13,13 +13,17 @@ namespace MapControl.Caching
|
|||
{
|
||||
public class ImageFileCache : IImageCache
|
||||
{
|
||||
private readonly StorageFolder folder;
|
||||
private readonly string folderPath;
|
||||
|
||||
public ImageFileCache(StorageFolder folder)
|
||||
: this(folder.Path)
|
||||
{
|
||||
this.folder = folder ?? throw new ArgumentNullException(nameof(folder));
|
||||
}
|
||||
|
||||
Debug.WriteLine("Created ImageFileCache in " + folder.Path);
|
||||
public ImageFileCache(string path)
|
||||
{
|
||||
folderPath = path;
|
||||
Debug.WriteLine("Created ImageFileCache in " + folderPath);
|
||||
}
|
||||
|
||||
public async Task<ImageCacheItem> GetAsync(string key)
|
||||
|
|
@ -36,6 +40,7 @@ namespace MapControl.Caching
|
|||
return null;
|
||||
}
|
||||
|
||||
var folder = await StorageFolder.GetFolderFromPathAsync(folderPath);
|
||||
var item = await folder.TryGetItemAsync(path);
|
||||
|
||||
if (item != null && item.IsOfType(StorageItemTypes.File))
|
||||
|
|
@ -68,7 +73,7 @@ namespace MapControl.Caching
|
|||
|
||||
try
|
||||
{
|
||||
var folder = this.folder;
|
||||
var folder = await StorageFolder.GetFolderFromPathAsync(folderPath);
|
||||
|
||||
for (int i = 0; i < folders.Length - 1; i++)
|
||||
{
|
||||
|
|
@ -88,12 +93,12 @@ namespace MapControl.Caching
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageFileCache: Writing {0}: {1}", Path.Combine(folder.Path, Path.Combine(folders)), ex.Message);
|
||||
Debug.WriteLine("ImageFileCache: Writing {0}: {1}", Path.Combine(folderPath, Path.Combine(folders)), ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string[] GetPathElements(string key)
|
||||
private static string[] GetPathElements(string key)
|
||||
{
|
||||
return key.Split('\\', '/', ',', ':', ';');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,54 +3,20 @@
|
|||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
#if WINUI
|
||||
using Microsoft.System;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
#else
|
||||
using Windows.UI.Core;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
#else
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public partial class Tile
|
||||
{
|
||||
public async Task SetImageAsync(Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
|
||||
async void callback()
|
||||
{
|
||||
try
|
||||
{
|
||||
SetImage(await loadImageFunc());
|
||||
tcs.SetResult(null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.SetException(ex);
|
||||
}
|
||||
}
|
||||
#if WINUI
|
||||
if (!Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, callback))
|
||||
{
|
||||
// should never happen, but just in case: reset Pending state and complete TaskCompletionSource
|
||||
Pending = true;
|
||||
tcs.SetResult(null);
|
||||
}
|
||||
#else
|
||||
_ = Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, callback);
|
||||
#endif
|
||||
_ = await tcs.Task.ConfigureAwait(false); // wait until image loading in UI thread is completed
|
||||
}
|
||||
|
||||
public void SetImage(ImageSource image, bool fadeIn = true)
|
||||
{
|
||||
Pending = false;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,13 @@ using System;
|
|||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml.Media;
|
||||
#else
|
||||
using Microsoft.System;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
|
|
@ -45,13 +52,55 @@ namespace MapControl
|
|||
|
||||
if (buffer != null && buffer.Length > 0)
|
||||
{
|
||||
await tile.SetImageAsync(() => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
await SetTileImageAsync(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static Task LoadTileAsync(Tile tile, TileSource tileSource)
|
||||
{
|
||||
return tile.SetImageAsync(() => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
||||
return SetTileImageAsync(tile, () => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
||||
}
|
||||
|
||||
#if WINDOWS_UWP
|
||||
public static async Task SetTileImageAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
|
||||
await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
tile.SetImage(await loadImageFunc());
|
||||
tcs.TrySetResult(null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
});
|
||||
|
||||
await tcs.Task.ConfigureAwait(false);
|
||||
}
|
||||
#else
|
||||
public static Task SetTileImageAsync(Tile tile, Func<Task<ImageSource>> loadImageFunc)
|
||||
{
|
||||
var tcs = new TaskCompletionSource();
|
||||
|
||||
tile.Image.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
tile.SetImage(await loadImageFunc());
|
||||
tcs.TrySetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
});
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue