C# removing layers & empty grouplayers

Discussion created by mratcliffe_mwwssb on Jun 22, 2018

Since I didn't see much out there (C#, SDK 2.1) on manipulating grouplayer, specifically removing them from the TOC if empty; here is an example.  Function "removes unchecked layers in the TOC" through a series of steps.


private async Task X_TOC_lyrs()
await ActMap();// checker


//Removes group-level layers that are unchecked, at once

IReadOnlyCollection<Layer> dlayers = MapView.Active.Map.Layers.Where(l => l.IsVisible == false).ToList();
await QueuedTask.Run(() => MapView.Active.Map.RemoveLayers(dlayers));


//Removes subgroup-level layers that are unchecked, one-by-one

IEnumerable<Layer> dlayers2 = MapView.Active.Map.GetLayersAsFlattenedList().Where(l => l.IsVisible == false).ToList();
foreach (var dlyr in dlayers2)
await QueuedTask.Run(() => MapView.Active.Map.RemoveLayer(dlyr));


//Remove empty groups

IReadOnlyList<GroupLayer> glayers = MapView.Active.Map.Layers.OfType<GroupLayer>().ToList();
foreach (var lyr in glayers)
if(lyr.Layers.Count == 0)
await QueuedTask.Run(() => MapView.Active.Map.RemoveLayer(lyr));