I'm trying to detect when the current MapView changes, so I can then activate/deactivate a button on a toolbar based on layers in the active map.
But when I click on (for example) the 'Catalog' tab, the ActiveMapViewChangedEvent fires and I get a NullReferenceException. As a workaround, I just catch the exception and disable the tool.
Is there a better way to determine if the user is no longer on a 'MapView' tab??
var lyr = MapView.Active.Map.FindLayers("SDEDB1.GISADMIN.ADDR_Durham").FirstOrDefault();
if (lyr == null)
When the ActiveMapViewChanged event fires, you can check the Event args to see if the "IncomingView" arg is null.
Like this code snippet:
private async void OnActiveMapViewChanged
if (activeMapViewChangedEventArgs.IncomingView == null) return;
In addition you should never assume that there is always a mapview active. What if your user has a layout active? What if your user has no mapviews open. In both of these scenarios, MapView.Active will be null and MapView.Active.Map will throw a nullReferenceException as you are experiencing.
Rather than wrap in a try, catch. you should add the following to your code
if (MapView.Active == null) return;