I am building an add-in to add/remove data from comboboxes and have encountered some issues.
Background: I have a repository of .lyr files that point to source data (in all formats: raster, vector) in various locations (file gdb, sde), so I built a combobox for each category to display a list of the layer name:
For the OnSelectionChange() I check if the layer selected exists by passing the selected "item.Text" as a key to a dictionary. If it does not exist, I return the path (its value) to a constructor so it is added to the active map and if it does exist, I want to remove the layer:
protected async override void OnSelectionChange(ComboBoxItem item)
if (item == null)
var lyrExists = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Any(f => f.Name == item.Text);
IDictionary<string, string> Planning_ = new Dictionary<string, string>();
Boundaries_.Add("94 Page Index btn8", @"\\Ceims\gd3\Layers\Boundaries\94 Page Index btn8.lyr");
Boundaries_.Add("94 Page Index", @"\\Ceims\gd3\Layers\Boundaries\94 Page Index.lyr");
Boundaries_.Add(rest of dictionary)...
Map map = MapView.Active.Map;
string pathkey = item.Text;
string pathval = "";
if (Planning_.TryGetValue(pathkey, out pathval))
string url = pathval;
Uri uri = new Uri(url);
await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map));
var lyrExists = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(f => f.Name == item.Text);
var lyrtoremove = lyrExists; // MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(f => f.Name == item.Text);
await QueuedTask.Run(() => MapView.Active.Map.RemoveLayers(lyrtoremove));
In most cases the code works, yet I am having the following issues
Adding raster imagery - I get a User-unhandled exception: (path to my .lyr file) cannot be used to create a layer. Layer type is not supported.' What command can I use to add raster data from an lyr file?
When re-selecting a group layer I have added, another copy of the group layer is added instead of removed. Feature Layers work just fine, what is the best way to screen and remove group layers given this logic?
If I am on the catalog pane and try to add a layer it crashes, do I need to set a state?
Thanks in advance!