diff --git a/MapControl/ImageTileSource.cs b/MapControl/ImageTileSource.cs new file mode 100644 index 00000000..a61cf1d2 --- /dev/null +++ b/MapControl/ImageTileSource.cs @@ -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 +{ + /// + /// 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. + /// + 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)); + } + } +} diff --git a/MapControl/Location.cs b/MapControl/Location.cs index 82218306..79a86956 100644 --- a/MapControl/Location.cs +++ b/MapControl/Location.cs @@ -57,6 +57,9 @@ namespace MapControl } } + /// + /// Converts from string to Location. + /// public class LocationConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) diff --git a/MapControl/LocationCollection.cs b/MapControl/LocationCollection.cs index e48a84f7..50296464 100644 --- a/MapControl/LocationCollection.cs +++ b/MapControl/LocationCollection.cs @@ -41,6 +41,9 @@ namespace MapControl } } + /// + /// Converts from string to LocationCollection. + /// public class LocationCollectionConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) diff --git a/MapControl/MapControl.csproj b/MapControl/MapControl.csproj index 7ee5737c..7a9ed90e 100644 --- a/MapControl/MapControl.csproj +++ b/MapControl/MapControl.csproj @@ -46,6 +46,7 @@ + diff --git a/MapControl/TileImageLoader.cs b/MapControl/TileImageLoader.cs index 41c45973..c90b5550 100644 --- a/MapControl/TileImageLoader.cs +++ b/MapControl/TileImageLoader.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Net; using System.Runtime.Caching; using System.Threading; +using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Threading; @@ -84,18 +85,9 @@ namespace MapControl private void BeginGetTilesAsync(object newTilesList) { List newTiles = (List)newTilesList; + ImageTileSource imageTileSource = tileLayer.TileSource as ImageTileSource; - if (tileLayer.TileSource is ImageTileSource) - { - 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 (imageTileSource == null) { if (Cache == null) { @@ -143,6 +135,22 @@ namespace MapControl ThreadPool.QueueUserWorkItem(DownloadTiles); } } + else if (imageTileSource.CanLoadAsync) + { + foreach (Tile tile in newTiles) + { + tileLayer.Dispatcher.BeginInvoke(DispatcherPriority.Background, + (Action)((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)(t => t.SetSource(imageTileSource.LoadImage(t.XIndex, t.Y, t.ZoomLevel))), tile); + } + } } private void DownloadTiles(object o) @@ -188,7 +196,7 @@ namespace MapControl return false; } - tileLayer.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() => tile.SetSource(bitmap))); + tileLayer.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(t => t.SetSource(bitmap)), tile); return true; } diff --git a/MapControl/TileSource.cs b/MapControl/TileSource.cs index 59ebf953..9b436f96 100644 --- a/MapControl/TileSource.cs +++ b/MapControl/TileSource.cs @@ -7,15 +7,13 @@ using System.ComponentModel; using System.Globalization; using System.Text; using System.Windows; -using System.Windows.Media; -using System.Windows.Media.Imaging; namespace MapControl { /// /// Provides the URI of a map tile. /// - [TypeConverter(typeof(TileSourceTypeConverter))] + [TypeConverter(typeof(TileSourceConverter))] public class TileSource { private Func getUri; @@ -156,23 +154,10 @@ namespace MapControl } } - /// - /// 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. - /// - public class ImageTileSource : TileSource - { - public virtual ImageSource GetImage(int x, int y, int zoomLevel) - { - return new BitmapImage(GetUri(x, y, zoomLevel)); - } - } - /// /// Converts from string to TileSource. /// - public class TileSourceTypeConverter : TypeConverter + public class TileSourceConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {