/// <summary> /// Updates the <see cref="MeasurableSubLayers"/> property based on the state of the sub-layers within /// the current <see cref="MeasureLayer"/>. Will also update <see cref="MeasureSubLayer"/> if the /// current one is in an unmeasurable state. /// </summary> private void UpdateMeasurableSubLayers() { // Dynamic map service layer containing sub-layers ArcGISDynamicMapServiceLayer parentLayer = (ArcGISDynamicMapServiceLayer)MeasureLayer; // index of the current measure sub-layer among the map service's sub-layers int indexInMapService = parentLayer.Layers.IndexOf(MeasureSubLayer); // index of the measure sub-layer among the set of measurable sub-layers int indexInMeasurableLayers = MeasurableSubLayers.IndexOf(MeasureSubLayer); // index to insert newly measurable sub-layers int insertIndex = indexInMeasurableLayers + 1; // tracks whether the current measure sub-layer has become unmeasurable bool needsMeasureLayerUpdate = !MeasureSubLayer.IsMeasurable(parentLayer, Map); // Iterate through the sub-layers in the parent map service that are after // the current measure sub-layer for (int i = indexInMapService + 1; i < parentLayer.Layers.Length; i++) { // Get the sub-layer at the current loop index LayerInfo subLayer = parentLayer.Layers[i]; // Get whether the loop sub-layer is measurable and is already a member // of the measurable sub-layers collection bool isMeasurable = subLayer.IsMeasurable(parentLayer, Map); bool isInCollection = MeasurableSubLayers.Contains(subLayer); if (isMeasurable && !isInCollection) // sub-layer is measurable but not in { // collection, so it needs to be added. MeasurableSubLayers.Insert(insertIndex, subLayer); if (needsMeasureLayerUpdate) // check whether a new measure sub-layer needs to be selected { // Update the measure sub-layer and reset the update flag MeasureSubLayer = subLayer; needsMeasureLayerUpdate = false; } insertIndex++; // Increment the insertion index so the next sub-layer is inserted // after this one } else if (!isMeasurable && isInCollection) // sub-layer in collection has become { // unmeasurable, so it needs to be removed MeasurableSubLayers.Remove(subLayer); } else if (isMeasurable && isInCollection) // sub-layer is measurable and already in { // collection. if (needsMeasureLayerUpdate) { MeasureSubLayer = subLayer; // Update the sub-layer if necessary and needsMeasureLayerUpdate = false; // reset the update flag. } insertIndex++; // Increment insertion index so the next one } // gets inserted after this one. } insertIndex = indexInMeasurableLayers; // Iterate through the sub-layers in the parent map service that are before the current // measure sub-layer, looping through them in reverse order for (int i = indexInMapService - 1; i > -1; i--) { // Get the sub-layer at the current loop index LayerInfo subLayer = parentLayer.Layers[i]; // Get whether the loop sub-layer is measurable and is already a member // of the measurable sub-layers collection bool isMeasurable = subLayer.IsMeasurable(parentLayer, Map); bool isInCollection = MeasurableSubLayers.Contains(subLayer); if (isMeasurable && !isInCollection) // sub-layer is measurable but not in { // collection, so it needs to be added. MeasurableSubLayers.Insert(insertIndex, subLayer); if (needsMeasureLayerUpdate) // check whether a new measure sub-layer needs to be selected { // Update the measure sub-layer and reset the update flag MeasureSubLayer = subLayer; needsMeasureLayerUpdate = false; } insertIndex--; // Decrement the insertion index so the next sub-layer is inserted // before this one } else if (!isMeasurable && isInCollection) // sub-layer in collection has become { // unmeasurable, so it needs to be removed MeasurableSubLayers.Remove(subLayer); } else if (isMeasurable && isInCollection) // sub-layer is measurable and already in { // collection. if (needsMeasureLayerUpdate) { MeasureSubLayer = subLayer; // Update the sub-layer if necessary and needsMeasureLayerUpdate = false; // reset the update flag. } insertIndex--; // Decrement insertion index so the next one } // gets inserted before this one. } // Check whether original measure sub-layer needs to be removed if (!parentLayer.Layers[indexInMapService].IsMeasurable(parentLayer, Map)) { MeasurableSubLayers.Remove(parentLayer.Layers[indexInMapService]); } // If there are no more measurable sub-layers in the current measure layer, update // the list of measurable layers. if (MeasurableSubLayers.Count == 0) { UpdateMeasurableLayers(); } }