mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-06 06:55:04 +00:00
Version 4.10.0: Updated target framework versions. Cleanup of TypeConverters, ImageLoader, MBTileSource.
This commit is contained in:
parent
63a4c1f0a7
commit
6a1653056f
36 changed files with 272 additions and 292 deletions
|
|
@ -15,67 +15,25 @@ using System.Windows.Media.Imaging;
|
|||
|
||||
namespace MapControl
|
||||
{
|
||||
public static class ImageLoader
|
||||
public static partial class ImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// The HttpClient instance used when image data is downloaded from a web resource.
|
||||
/// </summary>
|
||||
public static HttpClient HttpClient { get; set; } = new HttpClient();
|
||||
|
||||
public static async Task<ImageSource> LoadImageAsync(Uri uri, bool isTileImage)
|
||||
{
|
||||
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
|
||||
{
|
||||
return await LoadLocalImageAsync(uri);
|
||||
}
|
||||
|
||||
if (uri.Scheme == "http")
|
||||
{
|
||||
return await LoadHttpImageAsync(uri, isTileImage);
|
||||
}
|
||||
|
||||
return new BitmapImage(uri);
|
||||
}
|
||||
|
||||
public static Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
ImageSource imageSource = null;
|
||||
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
||||
|
||||
if (!File.Exists(path))
|
||||
if (File.Exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
return (ImageSource)BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadHttpImageAsync(Uri uri, bool isTileImage)
|
||||
{
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (!isTileImage || IsTileAvailable(response.Headers))
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
using (var stream = File.OpenRead(path))
|
||||
{
|
||||
await response.Content.CopyToAsync(stream);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
|
||||
imageSource = CreateImageSource(stream);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return imageSource;
|
||||
});
|
||||
}
|
||||
|
||||
public static async Task<bool> LoadHttpTileImageAsync(Uri uri, Func<MemoryStream, TimeSpan?, Task> tileCallback)
|
||||
|
|
@ -88,22 +46,46 @@ namespace MapControl
|
|||
}
|
||||
else if (IsTileAvailable(response.Headers))
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
await response.Content.CopyToAsync(stream);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
await response.Content.CopyToAsync(stream);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
await tileCallback(stream, response.Headers.CacheControl?.MaxAge);
|
||||
await tileCallback(stream, response.Headers.CacheControl?.MaxAge);
|
||||
}
|
||||
}
|
||||
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
|
||||
public static ImageSource CreateImageSource(Stream stream)
|
||||
{
|
||||
var bitmapImage = new BitmapImage();
|
||||
bitmapImage.BeginInit();
|
||||
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
||||
bitmapImage.StreamSource = stream;
|
||||
bitmapImage.EndInit();
|
||||
bitmapImage.Freeze();
|
||||
return bitmapImage;
|
||||
}
|
||||
|
||||
public static Task<ImageSource> CreateImageSourceAsync(Stream stream)
|
||||
{
|
||||
return Task.Run(() => CreateImageSource(stream));
|
||||
}
|
||||
|
||||
private static async Task<Stream> GetResponseStreamAsync(HttpContent content)
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
await content.CopyToAsync(stream);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return stream;
|
||||
}
|
||||
|
||||
private static bool IsTileAvailable(HttpResponseHeaders responseHeaders)
|
||||
{
|
||||
IEnumerable<string> tileInfo;
|
||||
|
||||
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,11 +9,12 @@
|
|||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MapControl</RootNamespace>
|
||||
<AssemblyName>MapControl.WPF</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<TargetFrameworkProfile />
|
||||
<TargetFrameworkProfile>
|
||||
</TargetFrameworkProfile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
|
@ -25,6 +26,7 @@
|
|||
<WarningLevel>4</WarningLevel>
|
||||
<NoWarn>
|
||||
</NoWarn>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>none</DebugType>
|
||||
|
|
@ -33,6 +35,7 @@
|
|||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
|
|
@ -86,6 +89,9 @@
|
|||
<Compile Include="..\Shared\HyperlinkText.cs">
|
||||
<Link>HyperlinkText.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\ImageLoader.cs">
|
||||
<Link>ImageLoader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\Intersections.cs">
|
||||
<Link>Intersections.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ namespace MapControl
|
|||
{
|
||||
latLabels.Add(new Label(lat, new FormattedText(
|
||||
GetLabelText(lat, labelFormat, "NS"),
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground)));
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, 1d)));
|
||||
|
||||
drawingContext.DrawLine(pen,
|
||||
projection.LocationToViewportPoint(new Location(lat, boundingBox.West)),
|
||||
|
|
@ -83,7 +83,7 @@ namespace MapControl
|
|||
{
|
||||
lonLabels.Add(new Label(lon, new FormattedText(
|
||||
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground)));
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, 1d)));
|
||||
|
||||
drawingContext.DrawLine(pen,
|
||||
projection.LocationToViewportPoint(new Location(boundingBox.South, lon)),
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ using System.Windows;
|
|||
[assembly: AssemblyCompany("Clemens Fischer")]
|
||||
[assembly: AssemblyCopyright("© 2018 Clemens Fischer")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyVersion("4.9.0")]
|
||||
[assembly: AssemblyFileVersion("4.9.0")]
|
||||
[assembly: AssemblyVersion("4.10.0")]
|
||||
[assembly: AssemblyFileVersion("4.10.0")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
[assembly: ComVisible(false)]
|
||||
|
|
|
|||
|
|
@ -9,11 +9,10 @@ using System.Net;
|
|||
using System.Runtime.Caching;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public partial class TileImageLoader : ITileImageLoader
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// Default folder path where an ObjectCache instance may save cached data,
|
||||
|
|
@ -44,12 +43,12 @@ namespace MapControl
|
|||
{
|
||||
try
|
||||
{
|
||||
loaded = await ImageLoader.LoadHttpTileImageAsync(uri, async (stream, maxAge) =>
|
||||
{
|
||||
await SetTileImageAsync(tile, stream); // create BitmapFrame before caching
|
||||
|
||||
SetCachedImage(cacheKey, stream, GetExpiration(maxAge));
|
||||
});
|
||||
loaded = await ImageLoader.LoadHttpTileImageAsync(uri,
|
||||
async (stream, maxAge) =>
|
||||
{
|
||||
await SetTileImageAsync(tile, stream); // create BitmapImage before caching
|
||||
SetCachedImage(cacheKey, stream, GetExpiration(maxAge));
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -68,7 +67,7 @@ namespace MapControl
|
|||
|
||||
private async Task SetTileImageAsync(Tile tile, MemoryStream stream)
|
||||
{
|
||||
var imageSource = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
|
||||
var imageSource = ImageLoader.CreateImageSource(stream);
|
||||
|
||||
await tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(imageSource));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,12 +21,6 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
[TypeConverter(typeof(LocationConverter))]
|
||||
[Serializable]
|
||||
public partial class Location
|
||||
{
|
||||
}
|
||||
|
||||
public class LocationCollectionConverter : TypeConverter
|
||||
{
|
||||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
||||
|
|
@ -40,11 +34,6 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
[TypeConverter(typeof(LocationCollectionConverter))]
|
||||
public partial class LocationCollection
|
||||
{
|
||||
}
|
||||
|
||||
public class BoundingBoxConverter : TypeConverter
|
||||
{
|
||||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
||||
|
|
@ -58,12 +47,6 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
[TypeConverter(typeof(BoundingBoxConverter))]
|
||||
[Serializable]
|
||||
public partial class BoundingBox
|
||||
{
|
||||
}
|
||||
|
||||
public class TileSourceConverter : TypeConverter
|
||||
{
|
||||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
||||
|
|
@ -76,9 +59,4 @@ namespace MapControl
|
|||
return new TileSource { UriFormat = value as string };
|
||||
}
|
||||
}
|
||||
|
||||
[TypeConverter(typeof(TileSourceConverter))]
|
||||
public partial class TileSource
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue