ArcGIS Pro 3.3 API Reference Guide
ArcGIS.Desktop.Core.Events Namespace / ProjectItemsChangedEvent Class / Subscribe Method
The delegate that is subscribed to the event ProjectItemsChangedEventArgs
(optional) True indicates the delegate has a strong reference to the event, and must be unsubscribed when the subscription is no longer needed. False indicates a weak delegate reference to the event. (default value = false)
Example

In This Topic
    Subscribe Method (ProjectItemsChangedEvent)
    In This Topic
    Subscribes to the ProjectItemsChangedEvent
    Syntax

    Parameters

    action
    The delegate that is subscribed to the event ProjectItemsChangedEventArgs
    keepSubscriberAlive
    (optional) True indicates the delegate has a strong reference to the event, and must be unsubscribed when the subscription is no longer needed. False indicates a weak delegate reference to the event. (default value = false)

    Return Value

    Example
    LayoutAdded_ProjectItemsChangedEvent_Add
    //Report the event args when a layout is added.
    
    //At 2.x - ArcGIS.Desktop.Layouts.Events.LayoutAddedEvent.Subscribe((args) =>
    //{
    //  System.Windows.MessageBox.Show("LayoutAddedEvent:" +
    //    Environment.NewLine +
    //    "   arg Layout: " + args.Layout.Name);
    //});
    
    //Use ProjectItemsChangedEvent at 3.x
    ArcGIS.Desktop.Core.Events.ProjectItemsChangedEvent.Subscribe((args) => {
      //Layout added. Layout removed would be NotifyCollectionChangedAction.Remove
      if (args.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add &&
          args.ProjectItem is LayoutProjectItem layoutProjectItem)
      {
        var layout_name = layoutProjectItem.Name; 
        var layout = layoutProjectItem.GetLayout();
        System.Diagnostics.Debug.WriteLine($"Layout Added: {layout_name}");
      }
    });
    
    LayoutRemoved_ProjectItemsChangedEvent_Remove
    //Report the event args when a layout is removed.
    
    //At 2.x - ArcGIS.Desktop.Layouts.Events.LayoutRemovedEvent.Subscribe((args) =>
    //{
    //  System.Windows.MessageBox.Show("LayoutViewEvent:" +
    //  Environment.NewLine +
    //  "   arg Layout: " + args.Layout.Name);
    //});
    
    //Use ProjectItemsChangedEvent at 3.x
    ArcGIS.Desktop.Core.Events.ProjectItemsChangedEvent.Subscribe((args) => {
      //Layout added. Layout removed would be NotifyCollectionChangedAction.Remove
      if (args.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove &&
          args.ProjectItem is LayoutProjectItem layoutProjectItem)
      {
        var layout_name = layoutProjectItem.Name;
        System.Diagnostics.Debug.WriteLine($"Layout Removed: {layout_name}");
      }
    });
    
    LayoutRemoved_ProjectItemsChangedEvent_Removing
    //Report the event args when a layout is about to be removed.
    
    //At 2.x - ArcGIS.Desktop.Layouts.Events.LayoutRemovingEvent.Subscribe((args) =>
    //{
    //  if (args.LayoutPath == "CIMPATH=layout.xml")
    //  {
    //    args.Cancel = true;
    //  }
    //  return Task.FromResult(0);
    //});
    
    //At 3.x use ProjectItemRemovingEvent
    ArcGIS.Desktop.Core.Events.ProjectItemRemovingEvent.Subscribe((args) =>
    {
      var layoutItems = args.ProjectItems.ToList().OfType<LayoutProjectItem>() ?? new List<LayoutProjectItem>();
      var layoutName = "DontDeleteThisOne";
      foreach(var layoutItem in layoutItems)
      {
        if (layoutItem.Name == layoutName)
        {
          args.Cancel = true;//Cancel the remove
          break;
        }
      }
      return Task.FromResult(0);
    });
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also