ActiveMapViewChangedEvent

984
3
04-12-2018 08:20 AM
BrianBulla
Occasional Contributor III

Hi,

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??

            ActiveMapViewChangedEvent.Subscribe((args) =>
            {
                try
                {
                    var lyr = MapView.Active.Map.FindLayers("SDEDB1.GISADMIN.ADDR_Durham").FirstOrDefault();
                    if (lyr == null)
                        FrameworkApplication.State.Deactivate(StateID);
                    else
                        FrameworkApplication.State.Activate(StateID);
                }
                catch (NullReferenceException)
                {
                    FrameworkApplication.State.Deactivate(StateID);
                }                
            });
0 Kudos
3 Replies
UmaHarano
Esri Regular Contributor

Hi Brian

When the ActiveMapViewChanged event fires, you can check the Event args to see if the "IncomingView" arg is null.

Like this code snippet:

ActiveMapViewChangedEvent.Subscribe(OnActiveMapViewChanged);

private async void OnActiveMapViewChanged
            (ActiveMapViewChangedEventArgs activeMapViewChangedEventArgs)
   {

      if (activeMapViewChangedEventArgs.IncomingView == null) return;
      ....
   }
NarelleChedzey
Esri Contributor

Brian, 

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;

BrianBulla
Occasional Contributor III

Great....thanks Uma and Narelle for your help.  Both methods work as I need them to.

0 Kudos