Layer not initialized

2421
2
Jump to solution
06-06-2012 09:51 AM
LuisGarcia2
Occasional Contributor II
I am uploading some layers dynamically and this is the code I am using for that:

ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer();                      layer.Url = myUrl; //this is just a string I acquire somewhere else  layer.InitializationFailed += new EventHandler<EventArgs>(layer_InitializationFailed); //I added this in case our service was down  layer.Initialize(); //I initialized here and...  if (layer.IsInitialized) //when I test in this line it says false       map1.Layers.Insert(1, layer);  ......  void layer_InitializationFailed(object sender, EventArgs e) {             MessageBox.Show("Your Layer was not initialized properly. The service may be down or there is a network problem."); } 


As you notice in the code, I initialize the layer and then test to make sure it has been initialized otherwise I do not add the layer. I did this since our internal service is sometimes down for maintenance. That way we avoid crashes or errors in the application. The interesting thing is that this code was working fine before. Now it does not add the layer to the map because it is always returning false and there is no error being handle either, no message box comes up ever.
Any ideas??
Thanks
0 Kudos
1 Solution

Accepted Solutions
JenniferNery
Esri Regular Contributor
Initialize is an asynchronous operation, which means you have to do the code in your Initialized event handler.


   ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer() { Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer" };    layer.Initialized += (a, b) =>     {      if (layer.IsInitialized && layer.InitializationFailure == null)       map1.Layers.Insert(1, layer);     };       layer.Initialize();


Is equivalent to:
   ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer() { Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer" };    layer.Initialized += new EventHandler<EventArgs>(layer_Initialized);      layer.Initialize();   }    void layer_Initialized(object sender, EventArgs e)   {    var layer = sender as Layer;    if (layer.IsInitialized && layer.InitializationFailure == null)     map1.Layers.Insert(1, layer);   }

View solution in original post

0 Kudos
2 Replies
JenniferNery
Esri Regular Contributor
Initialize is an asynchronous operation, which means you have to do the code in your Initialized event handler.


   ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer() { Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer" };    layer.Initialized += (a, b) =>     {      if (layer.IsInitialized && layer.InitializationFailure == null)       map1.Layers.Insert(1, layer);     };       layer.Initialize();


Is equivalent to:
   ArcGISDynamicMapServiceLayer layer = new ArcGISDynamicMapServiceLayer() { Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer" };    layer.Initialized += new EventHandler<EventArgs>(layer_Initialized);      layer.Initialize();   }    void layer_Initialized(object sender, EventArgs e)   {    var layer = sender as Layer;    if (layer.IsInitialized && layer.InitializationFailure == null)     map1.Layers.Insert(1, layer);   }
0 Kudos
LuisGarcia2
Occasional Contributor II
Jennifer,
thanks! That works great.
0 Kudos