XAML-Map-Control/MapsforgeTiles/WinUI/MapsforgeTileSource.WinUI.cs

50 lines
1.4 KiB
C#
Raw Normal View History

2026-02-15 00:09:21 +01:00
using System;
using System.IO;
2026-02-19 17:20:09 +01:00
using System.Runtime.InteropServices;
2026-02-15 00:09:21 +01:00
using System.Runtime.InteropServices.WindowsRuntime;
2026-02-19 17:20:09 +01:00
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
2026-02-15 00:09:21 +01:00
#if UWP
2026-02-19 17:20:09 +01:00
using Windows.UI.Xaml.Media;
2026-02-15 00:09:21 +01:00
using Windows.UI.Xaml.Media.Imaging;
#elif WINUI
2026-02-19 17:20:09 +01:00
using Microsoft.UI.Xaml.Media;
2026-02-15 00:09:21 +01:00
using Microsoft.UI.Xaml.Media.Imaging;
#endif
namespace MapControl.MapsforgeTiles
{
public partial class MapsforgeTileSource
{
2026-02-19 17:20:09 +01:00
public override async Task<ImageSource> LoadImageAsync(int zoomLevel, int column, int row)
2026-02-15 00:09:21 +01:00
{
2026-02-19 17:20:09 +01:00
ImageSource image = null;
var size = TileRenderer.TileSize;
2026-02-15 00:09:21 +01:00
var bitmap = new WriteableBitmap(size, size);
using var stream = bitmap.PixelBuffer.AsStream();
2026-02-19 17:20:09 +01:00
try
{
// Run a Task because in WinUI/UWP LoadImageAsync is called in the UI thread.
//
await Task.Run(() =>
{
var pixels = tileRenderer.RenderTile(zoomLevel, column, row);
if (pixels != null)
{
stream.Write(MemoryMarshal.AsBytes(pixels.AsSpan()));
image = bitmap;
}
});
}
catch (Exception ex)
2026-02-15 00:09:21 +01:00
{
2026-02-19 17:20:09 +01:00
Logger?.LogError(ex, "LoadImageAsync");
2026-02-15 00:09:21 +01:00
}
2026-02-19 17:20:09 +01:00
return image;
2026-02-15 00:09:21 +01:00
}
}
}