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.
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); }
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);
}
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);
}
}
// 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));
}
}
}
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);
}
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.
bool isVisible = westTexas.GetLayerVisibility(layerIds);
bool isVisible = westTexas.VisibleLayers.Contains(layerIds);