TransWikia.com

Listen to ArcGIS Pro project save event

Geographic Information Systems Asked by Dowlers on June 18, 2021

Is there a way to listen for the project save event in ArcGIS Pro? I need to check for something before allowing or denying the save event.

One Answer

This sample subscribes to project open, saving and saved events. Make sure to unsubscribe when no longer needed.

namespace N {        
    public class X {
       private SubscriptionToken _eventToken = null;
       private SubscriptionToken _eventToken2 = null;
       private SubscriptionToken _eventToken3 = null;
       
       public X() {
           Project proj = Project.Current;
           if (proj == null)
           {
             // Wait for project to open.
             _eventToken = ProjectOpenedEvent.Subscribe(OnProjectInitialized);              
           }
           else
           {
               ProjectEventArgs projEventAgs = new ProjectEventArgs(proj);
               OnProjectInitialized(projEventAgs);
           }
               _eventToken2 = ProjectSavingEvent.Subscribe(OnProjectSaving); 
               _eventToken3 = ProjectSavedEvent.Subscribe(OnProjectSaved); 
      }
        
      private void OnProjectInitialized(ProjectEventArgs project)
      {
          NotifyUser("Project has loaded", false);
        
          if (_eventToken != null)
          {
              ProjectOpenedEvent.Unsubscribe(_eventToken);
              _eventToken = null;
          }
      }
        
      private async Task<bool> OnProjectSaving(ProjectEventArgs project)
      {
          return await QueuedTask.Run(() =>
          {
              if (MessageBox.Show("Are you sure?", "Save Project?", 
                            MessageBoxButton.OKCancel) == MessageBoxResult.OK)
              {
                  return true;
              }
              return false;
          });
      }
        
      private void OnProjectSaved(ProjectEventArgs project)
      {
          MessageBox.Show("Project was saved");
      }
   }
}

Please note that when I tested this, the project was saved regardless of whether OnProjectSaving() returned true or false. This could be a bug in the framework, but is much more likely to be a bug in my code or understanding.

Answered by intotecho on June 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP