ActiveMapViewChangedEvent

260
3
04-12-2018 08:20 AM
Highlighted
Regular Contributor

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);
                }               
            });
Reply
0 Kudos
3 Replies
Highlighted
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;
      ....
   }
Highlighted
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;

Highlighted
Regular Contributor

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

Reply
0 Kudos