1

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.

1
  • I don't use .NET or the SDK so can't answer with a tested example (or any certainty that the following is actually useful), but from a simple search of the API documentation, you might want to check out the ProjectSavingEvent class.
    – user2856
    Nov 27, 2019 at 0:25

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.