I want to be able to open (activate) a MapPane with a specific map if it exists, or create it if it doesn't but can't figure out how to accomplish this.
Logically, I can get all the MapPanes with this:
var mapProjItems = Project.Current.GetItems<MapProjectItem>();
Each of the MapPanes in there *should* have a map, and if I iterate through there, I can activate the pane if the map's name is the one I want, or create a new one if it is not found. The problem is that only the MapPane that is currently active has a map, for all the others the map property is null.
There is a Pro Snippet called "Get the Unique List of Maps From the Map Panes" and it crashes because of the same problem:
public static IReadOnlyList<Map> GetMapsFromMapPanes()
{
//Gets the unique list of Maps from all the MapPanes.
//Note: The list of maps retrieved from the MapPanes
//maybe less than the total number of Maps in the project.
//It depends on what maps the user has actually opened.
var mapPanes = ProApp.Panes.OfType<IMapPane>()
.GroupBy((mp) => mp.MapView.Map.URI).Select(grp => grp.FirstOrDefault());
List<Map> uniqueMaps = new List<Map>();
foreach (var pane in mapPanes)
uniqueMaps.Add(pane.MapView.Map);
return uniqueMaps;
}
There is another snippet called "Find a map within a project and open it", but it always creates a new MapPane (so then you have two with the same name and map) because of the same problem.
And there is also the MapProjectItem.OpenMapPane() method, but it too creates a new MapPane.
This should be so simple to do...