mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Added TileImageLoader.WinUI
This commit is contained in:
parent
d4bc137408
commit
e096f97e85
|
|
@ -57,7 +57,7 @@
|
|||
<Version>6.2.12</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="System.Data.SQLite.Core">
|
||||
<Version>1.0.114.2</Version>
|
||||
<Version>1.0.114.3</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.114.2" />
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.114.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ namespace MapControl
|
|||
|
||||
line.SetBinding(Shape.StrokeProperty, this.GetBinding(nameof(Stroke)));
|
||||
line.SetBinding(Shape.StrokeThicknessProperty, this.GetBinding(nameof(StrokeThickness)));
|
||||
#if WINDOWS_UWP
|
||||
#if WINUI || WINDOWS_UWP
|
||||
label.SetBinding(TextBlock.ForegroundProperty, this.GetBinding(nameof(Foreground)));
|
||||
#endif
|
||||
Children.Add(line);
|
||||
|
|
|
|||
|
|
@ -3,14 +3,14 @@
|
|||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
#else
|
||||
#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
|
||||
|
||||
namespace MapControl
|
||||
|
|
|
|||
|
|
@ -4,13 +4,8 @@
|
|||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml.Media;
|
||||
#else
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
|
|
@ -70,7 +65,6 @@ namespace MapControl
|
|||
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>();
|
||||
|
|
@ -90,26 +84,5 @@ namespace MapControl
|
|||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,10 @@
|
|||
<Compile Include="..\UWP\*.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="..\UWP\TileImageLoader.UWP.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.ProjectReunion" Version="0.8.0" />
|
||||
<PackageReference Include="Microsoft.ProjectReunion.Foundation" Version="0.8.0" />
|
||||
|
|
|
|||
88
MapControl/WinUI/TileImageLoader.WinUI.cs
Normal file
88
MapControl/WinUI/TileImageLoader.WinUI.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2021 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
namespace Caching
|
||||
{
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<Tuple<byte[], DateTime>> GetAsync(string key);
|
||||
|
||||
Task SetAsync(string key, byte[] buffer, DateTime expiration);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// Default folder path where an IImageCache instance may save cached data, i.e. C:\ProgramData\MapControl\TileCache
|
||||
/// </summary>
|
||||
public static string DefaultCacheFolder
|
||||
{
|
||||
get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "TileCache"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The IImageCache implementation used to cache tile images. The default is null.
|
||||
/// </summary>
|
||||
public static Caching.IImageCache Cache { get; set; }
|
||||
|
||||
|
||||
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
|
||||
{
|
||||
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
||||
var buffer = cacheItem?.Item1;
|
||||
|
||||
if (cacheItem == null || cacheItem.Item2 < DateTime.UtcNow)
|
||||
{
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri).ConfigureAwait(false);
|
||||
|
||||
if (response != null) // download succeeded
|
||||
{
|
||||
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
||||
|
||||
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
//else System.Diagnostics.Debug.WriteLine("Cached: " + cacheKey);
|
||||
|
||||
if (buffer != null && buffer.Length > 0)
|
||||
{
|
||||
await SetTileImageAsync(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static Task LoadTileAsync(Tile tile, TileSource tileSource)
|
||||
{
|
||||
return SetTileImageAsync(tile, () => tileSource.LoadImageAsync(tile.XIndex, tile.Y, tile.ZoomLevel));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -86,7 +86,7 @@ namespace MapControl.Caching
|
|||
return command;
|
||||
}
|
||||
|
||||
private SQLiteCommand SetItemCommand(string key, DateTime expiration, byte[] buffer)
|
||||
private SQLiteCommand SetItemCommand(string key, byte[] buffer, DateTime expiration)
|
||||
{
|
||||
var command = new SQLiteCommand("insert or replace into items (key, expiration, buffer) values (@key, @exp, @buf)", connection);
|
||||
command.Parameters.AddWithValue("@key", key);
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ namespace MapControl.Caching
|
|||
{
|
||||
try
|
||||
{
|
||||
using (var command = SetItemCommand(key, expiration, buffer))
|
||||
using (var command = SetItemCommand(key, buffer, expiration))
|
||||
{
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ namespace MapControl.Caching
|
|||
|
||||
try
|
||||
{
|
||||
using (var command = SetItemCommand(key, cacheItem.Item2, cacheItem.Item1))
|
||||
using (var command = SetItemCommand(key, cacheItem.Item1, cacheItem.Item2))
|
||||
{
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,11 +19,14 @@ namespace WinUiApp
|
|||
{
|
||||
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control Test Application");
|
||||
|
||||
var appData = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl");
|
||||
var bingMapsApiKeyFile = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "BingMapsApiKey.txt");
|
||||
|
||||
TileImageLoader.Cache = new ImageFileCache(Path.Combine(appData, "TileCache"));
|
||||
BingMapsTileLayer.ApiKey = File.ReadAllText(Path.Combine(appData, "BingMapsApiKey.txt"))?.Trim();
|
||||
BingMapsTileLayer.ApiKey = File.ReadAllText(bingMapsApiKeyFile)?.Trim();
|
||||
|
||||
TileImageLoader.Cache = new ImageFileCache(TileImageLoader.DefaultCacheFolder);
|
||||
//TileImageLoader.Cache = new FileDbCache(TileImageLoader.DefaultCacheFolder);
|
||||
//TileImageLoader.Cache = new SQLiteCache(TileImageLoader.DefaultCacheFolder);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue