Added ImageTileSource.CanLoadAsync property. Changed Dispatcher.BeginInvoke calls in TileImageLoader.

This commit is contained in:
ClemensF 2012-11-07 17:48:15 +01:00
parent 1d5703d382
commit c28e9d73d9
6 changed files with 58 additions and 29 deletions

View file

@ -0,0 +1,29 @@
// WPF MapControl - http://wpfmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MapControl
{
/// <summary>
/// Provides the image of a map tile. ImageTileSource bypasses download and
/// cache processing in TileImageLoader. By overriding the LoadImage method,
/// an application can provide tile images from an arbitrary source.
/// If the CanLoadAsync property is true, the LoadImage method will be called
/// from a separate non-UI thread and must hence return a frozen ImageSource.
/// </summary>
public class ImageTileSource : TileSource
{
public virtual bool CanLoadAsync
{
get { return false; }
}
public virtual ImageSource LoadImage(int x, int y, int zoomLevel)
{
return new BitmapImage(GetUri(x, y, zoomLevel));
}
}
}

View file

@ -57,6 +57,9 @@ namespace MapControl
} }
} }
/// <summary>
/// Converts from string to Location.
/// </summary>
public class LocationConverter : TypeConverter public class LocationConverter : TypeConverter
{ {
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)

View file

@ -41,6 +41,9 @@ namespace MapControl
} }
} }
/// <summary>
/// Converts from string to LocationCollection.
/// </summary>
public class LocationCollectionConverter : TypeConverter public class LocationCollectionConverter : TypeConverter
{ {
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)

View file

@ -46,6 +46,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="GlyphRunText.cs" /> <Compile Include="GlyphRunText.cs" />
<Compile Include="ImageTileSource.cs" />
<Compile Include="Location.cs" /> <Compile Include="Location.cs" />
<Compile Include="LocationAnimation.cs" /> <Compile Include="LocationAnimation.cs" />
<Compile Include="LocationCollection.cs" /> <Compile Include="LocationCollection.cs" />

View file

@ -11,6 +11,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Runtime.Caching; using System.Runtime.Caching;
using System.Threading; using System.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Threading; using System.Windows.Threading;
@ -84,18 +85,9 @@ namespace MapControl
private void BeginGetTilesAsync(object newTilesList) private void BeginGetTilesAsync(object newTilesList)
{ {
List<Tile> newTiles = (List<Tile>)newTilesList; List<Tile> newTiles = (List<Tile>)newTilesList;
ImageTileSource imageTileSource = tileLayer.TileSource as ImageTileSource;
if (tileLayer.TileSource is ImageTileSource) if (imageTileSource == null)
{
ImageTileSource imageTileSource = (ImageTileSource)tileLayer.TileSource;
foreach (Tile tile in newTiles)
{
tileLayer.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action)(() => tile.SetSource(imageTileSource.GetImage(tile.XIndex, tile.Y, tile.ZoomLevel))));
}
}
else
{ {
if (Cache == null) if (Cache == null)
{ {
@ -143,6 +135,22 @@ namespace MapControl
ThreadPool.QueueUserWorkItem(DownloadTiles); ThreadPool.QueueUserWorkItem(DownloadTiles);
} }
} }
else if (imageTileSource.CanLoadAsync)
{
foreach (Tile tile in newTiles)
{
tileLayer.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action<Tile, ImageSource>)((t, s) => t.SetSource(s)), tile, imageTileSource.LoadImage(tile.XIndex, tile.Y, tile.ZoomLevel));
}
}
else
{
foreach (Tile tile in newTiles)
{
tileLayer.Dispatcher.BeginInvoke(DispatcherPriority.Background,
(Action<Tile>)(t => t.SetSource(imageTileSource.LoadImage(t.XIndex, t.Y, t.ZoomLevel))), tile);
}
}
} }
private void DownloadTiles(object o) private void DownloadTiles(object o)
@ -188,7 +196,7 @@ namespace MapControl
return false; return false;
} }
tileLayer.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() => tile.SetSource(bitmap))); tileLayer.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action<Tile>)(t => t.SetSource(bitmap)), tile);
return true; return true;
} }

View file

@ -7,15 +7,13 @@ using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.Text; using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MapControl namespace MapControl
{ {
/// <summary> /// <summary>
/// Provides the URI of a map tile. /// Provides the URI of a map tile.
/// </summary> /// </summary>
[TypeConverter(typeof(TileSourceTypeConverter))] [TypeConverter(typeof(TileSourceConverter))]
public class TileSource public class TileSource
{ {
private Func<int, int, int, Uri> getUri; private Func<int, int, int, Uri> getUri;
@ -156,23 +154,10 @@ namespace MapControl
} }
} }
/// <summary>
/// Provides the image of a map tile. ImageTileSource bypasses download and
/// cache processing in TileImageLoader. By overriding the GetImage method,
/// an application can provide tile images from an arbitrary source.
/// </summary>
public class ImageTileSource : TileSource
{
public virtual ImageSource GetImage(int x, int y, int zoomLevel)
{
return new BitmapImage(GetUri(x, y, zoomLevel));
}
}
/// <summary> /// <summary>
/// Converts from string to TileSource. /// Converts from string to TileSource.
/// </summary> /// </summary>
public class TileSourceTypeConverter : TypeConverter public class TileSourceConverter : TypeConverter
{ {
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{ {