Select to view content in your preferred language

ArcGIS Pro 2.4 add in that would execute after a project is loaded

1607
6
Jump to solution
08-12-2019 06:47 AM
FrankMartin1
Regular Contributor

I have an ArcGIS Pro 2.4 add in that I would like to have execute after a project is loaded, but does not require the user to click a button or select a tool.  Essentially, it would act like a onload function.   Is there any way to accomplish this with an add in?  

Thanks for your help,

Frank

0 Kudos
1 Solution

Accepted Solutions
FrankMartin1
Regular Contributor

Thanks to everyone for their suggestions.  I used Than's suggestion to subscribe to the ActiveMapViewChangedEvent in the module (se below), and it works.  Now, when a project loads the add in code is executed immediately without having to activate it through a button.  I used Uma's suggestions to "hide" the button by setting the the loadOnClick attribute to false in the button definition.  

Again, thanks to everyone for their help!

Regards,

Frank

        protected override bool Initialize()
        {
            ArcGIS.Desktop.Mapping.Events.ActiveMapViewChangedEvent.Subscribe(MapViewActivateEvent);
            return true;
        }

        private void MapViewActivateEvent(ActiveMapViewChangedEventArgs obj)
        {
            try
            {

               var layerEventListeners = new List<string>();

               var featureLayers = mapView.Map.Layers.OfType<FeatureLayer>();

...
            }
...
         }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
6 Replies
UmaHarano
Esri Regular Contributor

Hi Frank,

In your add-in's config.daml, set the  "autoLoad" attribute to true and the button element's loadOnClick attribute to false to accomplish this.

..
<modules>
<insertModule id="QueryBuilderControl_Module" className="Module1" autoLoad="true" caption="Module1">
....‍‍‍‍

 <controls>
  <!-- add your controls here -->
  <button id="QueryBuilderControl_DefinitionQueryDockPane_ShowButton" loadOnClick="false" caption="Show DefinitionQueryDockPane" ...>
          <tooltip heading="Show Dockpane">Show Dockpane<disabledText /></tooltip>
        </button>
 </controls>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks

Uma

FrankMartin1
Regular Contributor

Uma,

Thanks.  I've changed both the autoLoad and loadOnClick as you suggested, however, the button code does not execute until the button has been click.  

<modules>
 <insertModule id="EditRBIDGeneratorPro_Module" className="Module1" autoLoad="true" caption="Module1">

<button id="EditRBIDGeneratorPro_Button1" caption="RBID Activate" className="Button1" loadOnClick="false" ...>
 <tooltip heading="RBID Activate Tooltip">Click to ensure the automatic addition of a RBID during edit sessions<disabledText /></tooltip>

The code I'm trying to execute is within the the OnClick function.  Is that the issue?  

namespace EditRBIDGeneratorPro
{
 internal class Button1 : Button
 {
   private Incrementor uid;
   protected override async void OnClick()
   {
     try
     {
         var mapView = MapView.Active;
         if (mapView == null)
         {
             var startString = "No map view";
             MessageBox.Show(startString);
         }
         var featureLayers = mapView.Map.Layers.OfType<FeatureLayer>();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

....

     }
     catch (Exception ex)
     {
         var ErrorString = ex.Message;
         MessageBox.Show("An error occurred while updating attribute column data " + ErrorString);
  } 
}

Thanks again,

Frank

0 Kudos
ChristopherMoore
Esri Contributor

You'll probably want to subscribe to the project opened event in your EditRBIDGeneratorPro_Module.Initialize method, for example:

internal class EditRBIDGeneratorPro_Module : Module

{

protected override bool Initialize()

{

    ArcGIS.Desktop.Core.Events.ProjectOpenedEvent.Subscribe(async (args) => {
       // Call Desired Method
    });

}

- Chris
0 Kudos
UmaHarano
Esri Regular Contributor

Hi Frank,

The loadOnClick attribute only determines when the button should be created by the framework. 

Thanks

Uma

0 Kudos
by Anonymous User
Not applicable

Hi Frank,

Based on your button click event code, it seems you are trying to perform something when the mapview is active.

If so, 

Use this,

In module class, add these

protected override bool Initialize()
{
ArcGIS.Desktop.Mapping.Events.ActiveMapViewChangedEvent.Subscribe(this.MapViewActivateEvent);

return true;
}

private void MapViewActivateEvent(ActiveMapViewChangedEventArgs obj)
{
try
{
var mapView = MapView.Active;
if (mapView == null)
{
var startString = "No map view";
ProDialog.MessageBox.Show(startString);
}
var featureLayers = mapView.Map.Layers.OfType<FeatureLayer>();

}
catch (Exception ex)
{
var ErrorString = ex.Message;
ProDialog.MessageBox.Show("An error occurred while updating attribute column data " + ErrorString);
}
}

FrankMartin1
Regular Contributor

Thanks to everyone for their suggestions.  I used Than's suggestion to subscribe to the ActiveMapViewChangedEvent in the module (se below), and it works.  Now, when a project loads the add in code is executed immediately without having to activate it through a button.  I used Uma's suggestions to "hide" the button by setting the the loadOnClick attribute to false in the button definition.  

Again, thanks to everyone for their help!

Regards,

Frank

        protected override bool Initialize()
        {
            ArcGIS.Desktop.Mapping.Events.ActiveMapViewChangedEvent.Subscribe(MapViewActivateEvent);
            return true;
        }

        private void MapViewActivateEvent(ActiveMapViewChangedEventArgs obj)
        {
            try
            {

               var layerEventListeners = new List<string>();

               var featureLayers = mapView.Map.Layers.OfType<FeatureLayer>();

...
            }
...
         }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos