Select to view content in your preferred language

Target Sublayer Visibility in Code Behind

1874
13
Jump to solution
07-06-2012 11:23 AM
JaredWhite
Regular Contributor
Hi,
A few weeks ago I discussed how to target sublayer Opacity in code behind with Dominique, the following code is the product of that

                 private void toggleSubLayerOpacity()         {             LayerDrawingOptions layerDrawOptions = new LayerDrawingOptions();             layerDrawOptions.LayerID = 0;             (MyMap.Layers["MyLayer"] as ArcGISDynamicMapServiceLayer).LayerDrawingOptions =                 new LayerDrawingOptionsCollection() { layerDrawOptions };             (MyMap.Layers["MyLayer"] as ArcGISDynamicMapServiceLayer).VisibleLayers = new int[] { 0 };             if (layerDrawOptions.Opacity == 0)             {                 layerDrawOptions.Opacity = 1;             }             else             {                 layerDrawOptions.Opacity = 0;             }         }


This works, however, toggling sublayer visibility would work considerably better for my project versus opacity, as well as increase app performance. How would I go about doing this?
0 Kudos
13 Replies
JaredWhite
Regular Contributor
That's exactly what I had done to work around that problem. Thanks again for all the help on this, works like a dream now.

On a side note, and I know this is probably an extremely basic question (though I can't find a direct answer anywhere), but that's the problem with teaching yourself a programming language is the basics get passed over.
But how would I go about changing the layerID integer to an array so multiple layers can be toggled by one command instead of writing a single command for each layer toggled.
I.E. something like

        private void ToggleVisibility(int[] layerIds)
        {
            var westTexas = MyMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer;
            if (westTexas == null) return;
            bool isVisible = westTexas.VisibleLayers.Contains(layerIds);
            westTexas.SetLayerVisibility(layerId, !isVisible);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ToggleVisibility(0, 1 ,2, 3);
        }


instead of

        private void ToggleVisibility(int layerId)
        {
            var westTexas = MyMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer;
            if (westTexas == null) return;
            bool isVisible = westTexas.VisibleLayers.Contains(layerId);
            westTexas.SetLayerVisibility(layerId, !isVisible);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ToggleVisibility(0);
            ToggleVisibility(1);
            ToggleVisibility(2);
            ToggleVisibility(3);
            ToggleVisibility(4);
            ToggleVisibility(8);
        }
0 Kudos
JoeHershman
MVP Alum
I think the most standard way would be to setup an Enumerable with your LayerId array and loop through those


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            int[] layerIDs = new[] { 0, 1, 2, 3, 4, 8 };


            foreach ( var layerId in layerIDs )
            {
                ToggleVisibility(layerId);
            }
        }



One kind of fun way to learn new things and do it nice and clean would be to setup an Extension method to do it

    // class to hold extension method
    public static class ExtensionMethods
    {
         
        public static void ToggleVisibility(this ArcGISDynamicMapServiceLayer layer, IEnumerable<int> layerIds)
        {
            foreach (var layerId in layerIds)
            {
                layer.SetLayerVisibility(layerId, !layer.VisibleLayers.Contains(layerId));
            }
        }
    }




Then are button click could look like this


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var westTexas = Map.Layers["West Texas"] as ArcGISDynamicMapServiceLayer;
            if ( westTexas == null ) return;
            
            var layerIDs = new[] { 0, 1, 2, 3, 4, 8 };


            westTexas.ToggleVisibility(layerIDs);
        }



Happy coding
Thanks,
-Joe
0 Kudos
JaredWhite
Regular Contributor
Thanks joe
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I was playing around with this and I notice that after initialization the VisibleLayers collection is still null (which would explain the error). It is not until a method call has been made to SeLayerVisisibility has been made that this collection is actually available. This explains the behavior you see because clicking a layer on the Legend will make a call internally to SetLayerVisibility.

I checked this on both 3.0 and 2.4 and see the same behavior.

Personally, I would consider this a bug.


By convention, a null VisibleLayers means that the default layer visibilities are used.
So you should take care of the null case in your code and get the visibility from the LayerInfo DefaultVisibility.

Or simply use GetLayerVisibility which is doing the job for you.

i.e.

bool isVisible = westTexas.GetLayerVisibility(layerIds);


instead of
bool isVisible = westTexas.VisibleLayers.Contains(layerIds);
0 Kudos