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; } }
Solved! Go to Solution.
IEnumerable<int> myInts = westTexas.VisibleLayers.Except(new[] {0});
private void ToggleVisibility(int layerId) { var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if ( westTexas == null ) return; //this will return True if the VsibleLayers includes layerId (is Visible), or False if it is not included (not visible) bool isVisible = westTexas.VisibleLayers.Contains(layerId); //This will flip the visbility, if layerId was vsible it rurns it off, if it is not visible it turns it on. westTexas.SetLayerVisibility(layerId, !isVisible); }
private void togglecweiSublayer() { ArcGISDynamicMapServiceLayer westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; List<int> visibleLayerList = westTexas.VisibleLayers != null ? westTexas.VisibleLayers.ToList() : new List<int>(); if (!visibleLayerList.Contains(0)) { visibleLayerList.Remove(0); } else { visibleLayerList.Add(0); } westTexas.VisibleLayers = visibleLayerList.ToArray(); }
var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; bool isVisible = westTexas.VisibleLayers.Contains(0); westTexas.SetLayerVisibility(0, !isVisible);
The way to change visibility is to use the SetLayerVisibility method on the ArcGISDynamicMapServiceLayer instead of removing/adding from the VisibleLayers collections, something like.var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; bool isVisible = westTexas.VisibleLayers.Contains(0); westTexas.SetLayerVisibility(0, !isVisible);
Hope that helps
-Joe
Hi Joe,
Thanks for the response. It seems like this will work, but I'm getting an error saying System.Array does not contain a definition for 'Contains'. How would i fix this?
bool isNotVisible = westTexas.VisibleLayers.Except(0);
IEnumerable<int> myInts = westTexas.VisibleLayers.Except(new[] {0});
private void ToggleVisibility(int layerId) { var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if ( westTexas == null ) return; //this will return True if the VsibleLayers includes layerId (is Visible), or False if it is not included (not visible) bool isVisible = westTexas.VisibleLayers.Contains(layerId); //This will flip the visbility, if layerId was vsible it rurns it off, if it is not visible it turns it on. westTexas.SetLayerVisibility(layerId, !isVisible); }
Jared,
The Except() method is meant to return an IEnumerable<T> of all the elements in an IEnumerable<T> that are not a provided IEnumerable<T> (huh :)). So the usage is like thisIEnumerable<int> myInts = westTexas.VisibleLayers.Except(new[] {0});
Which returns an IEnumerable<int> of all the values in VisibleLayers Except for 0, which is not really what you want.
The code sample I provided should work to Toggle visibility of a given layer. You just need to use contains backwards, if .Contains returns false, then pass SetLayerVisibility a value of trueprivate void ToggleVisibility(int layerId) { var westTexas = TabletMap.Layers["West Texas"] as ArcGISDynamicMapServiceLayer; if ( westTexas == null ) return; //this will return True if the VsibleLayers includes layerId (is Visible), or False if it is not included (not visible) bool isVisible = westTexas.VisibleLayers.Contains(layerId); //This will flip the visbility, if layerId was vsible it rurns it off, if it is not visible it turns it on. westTexas.SetLayerVisibility(layerId, !isVisible); }
Hope that helps
-joe
Thanks Joe,
I didn't realize this was working because of a strange problem with the my app. The sublayerToggle you provided is being used as an action strung into a button click , but the app also has a Legend with checkboxes controlling the sublayer visibility in my app, when I toggle the layer on or off on the legend first, then the sublayerToggle also works with the button click, but if it hasn't been toggled with the legend, the toggle strung to the button click doesn't do anything. I'm very confused as to why it's doing this, but I'm also pretty sure it has nothing to do with the code you provided me at this point. Thank you very much for the help.
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); }
private void toggleSubLayerON(int sublayerId) { ArcGISDynamicMapServiceLayer layer= TabletMap.Layers["Layer"] as ArcGISDynamicMapServiceLayer; List<int> visibleLayerList = layer.VisibleLayers != null ? layer.VisibleLayers.ToList() : new List<int>(); visibleLayerList.Add(sublayerId); layer.VisibleLayers = visibleLayerList.ToArray(); } private void toggleSubLayerOFF(int sublayerID) { ArcGISDynamicMapServiceLayer layer= TabletMap.Layers["Layer"] as ArcGISDynamicMapServiceLayer; List<int> visibleLayerList = layer.VisibleLayers != null ? layer.VisibleLayers.ToList() : new List<int>(); visibleLayerList.Remove(sublayerID); layer.VisibleLayers = visibleLayerList.ToArray(); }