Changing basemap from service to tile cache does not work

Discussion created by minerjoe Champion on Jul 3, 2017
Latest reply on Jan 4, 2018 by mnielsen-esristaff

I have a situation where we have an offline tile cache used for a basemap for imagery.  We also want to be able to use the online streets basemap if connected.  This seems straightforward, however, does not work.  If I initially load a tiles layer as basemap I am unable to switch the basemap to a basemap that uses service layers and vice-versa.


So if I initially load tiles:


var tileCache = new TileCache(@"D:\Mobile\Imagery\Layers");
ArcGISTiledLayer imageLayer = new ArcGISTiledLayer(tileCache);
map = new Esri.ArcGISRuntime.Mapping.Map(new Basemap(imageLayer)) { InitialViewpoint = new Viewpoint(extent) };


The offline cache loads correctly.  However, when I try to switch the reference to a service based basemap


RuntimeControls.MapView.Map.Basemap = Basemap.CreateStreets();


The base map simply disappears (in the above RuntimeControls is used to share the MapView across the application.


If instead of the first code block I start with:


map = new Esri.ArcGISRuntime.Mapping.Map(Basemap.CreateImageryWithLabelsVector()) { InitialViewpoint = new Viewpoint(extent) };


Switching basemaps in the second block works fine.  The opposite behavior is also observed.  If I initially start with a Streets layer for basemap, I am unable to switch the basemap to the TileCache.  However, when I switch back to the TileCache after the failed attempt to change to streets the basemap is displayed.


The only thing that works is to create a new Map instance.  The problem with this is the OperationalLayers are lost.  I tried a workaround to see if I can recreate the map.  Along these lines



RuntimeControls.MapView.Map.Basemap = Basemap.CreateStreets();

LayerCollection operationalLayers = RuntimeControls.MapView.Map.OperationalLayers;
Envelope extent = RuntimeControls.MapView.VisibleArea.Extent;
RuntimeControls.MapView.Map = null;

Map map = new Map(Basemap.CreateStreets()) { InitialViewpoint = new Viewpoint(extent) };
foreach (var layer in operationalLayers.OfType<FeatureLayer>())
     FeatureLayer featureLayer = new FeatureLayer(layer.FeatureTable);



This, though, throws an exception: 

Esri.ArcGISRuntime.ArcGISRuntimeException was unhandled
Message=Object already owned.: Already owned.

I put the line setting the map to null in there hoping it would release the layer ownership, I even tried to force a garbage collection to see if that would release it, but no luck.


Any thoughts?  This is a pretty critical aspect to our application users need to be able to see a streets map when they are connected