On application close method

1203
10
09-19-2017 08:09 AM
DrewD
by
New Contributor III

Hello, I was wondering if there was a way to close an add-in pane when Arcpro is shutdown. As you know Arcpro remembers which tabs/windows were open in the previous session, and I don't want the add-in tab to be available when the application is reopened.

Are there any simple overrides that I could implement?

Alternatively on open would be fine too, I've been trying this.close(); and FrameworkApplication.Panes.ClosePane(this.InstanceID); but can't get them to work

0 Kudos
10 Replies
UmaHarano
Esri Regular Contributor

Hi Drew

You can accomplish this by using the ProjectClosingEvent.  This is the code that worked for me -

 private static async Task OnProjectClosingAsync(CancelEventArgs arg)
        {
            FrameworkApplication.Panes.ClosePane(MyPaneID);
            await Project.Current.SaveAsync();
          
        }

Thanks

Uma

0 Kudos
DrewD
by
New Contributor III

Thanks Uma, I'm trying it but not having any luck. MyPaneID is a unsigned int, would I find it using this.InstanceID; ?

0 Kudos
UmaHarano
Esri Regular Contributor

i Drew

You could have multiple instances (InstanceIDs) of your pane. So you can iterate through the Panes to get "your" panes only and then close them like this:

private static async Task OnProjectClosingAsync(CancelEventArgs arg)
        {
            IList<uint> myPaneInstanceIDs = new List<uint>();
            foreach (Pane pane in FrameworkApplication.Panes)
            {
                if (pane.ContentID == _viewPaneID) //this is the DAML id of your pane.  
                {
                    myPaneInstanceIDs.Add(pane.InstanceID); //InstanceID of your pane, could be multiple, so build the collection                    
                }
            }
            foreach (var instanceID in myPaneInstanceIDs) //close each of "your" panes.
            {
                FrameworkApplication.Panes.ClosePane(instanceID);
            }
            await Project.Current.SaveAsync(); //save the project.          
        }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DrewD
by
New Contributor III

Thank you again Uma, for some reason the pane still doesn't close, could there be a level of protection preventing it from being closed? I've attached a msg box below:

FrameworkApplication.Panes.ClosePane(instanceID);

ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Closed pane " + instanceID);

and it pops up a dialog saying "closed pane 9" but nothing is actually changes

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Drew

Can you please share the code where you get the instanceID of the pane?  

0 Kudos
DrewD
by
New Contributor III

I'm using the snippet you provided above, but for whatever reason the actual pane will not close

IList<uint> myPaneInstanceIDs = new List<uint>();
foreach (Pane pane in FrameworkApplication.Panes) {
   if (pane.ContentID == _viewPaneID) { //_viewPanelD is correct 
      myPaneInstanceIDs.Add(pane.InstanceID); //InstanceID of your pane, could be multiple, so build the collection
   }
}
foreach (var instanceID in myPaneInstanceIDs) { //close each of "your" panes.
   FrameworkApplication.Panes.ClosePane(instanceID);
   ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Closed pane " + instanceID);   //my addition, shows me that a pane is found
}

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Drew

Just a couple of things that could be checked -

1.  The _viewPaneID property holds the ID from the DAML for your pane, correct?  It should be "ClosePane_Pane1" from the example snippet from config.daml below.  

 <panes>
        <pane id="ClosePane_Pane1" caption="Pane 1" className="Pane1ViewModel" smallImage="Images\GenericButtonGreen16.png" defaultTab="esri_mapping_homeTab" defaultTool="esri_mapping_navigateTool">
          <content className="Pane1View" />
        </pane>
      </panes>

2.  While debugging your add-in when you place a breakpoint on this line below does it get hit? How many panes were open with that the _viewPaneID?  When a wrong InstanceID is passed to ClosePane method, I do see that nothing happens (seems to crash silently with no exceptions). 

      myPaneInstanceIDs.Add(pane.InstanceID); //InstanceID of your pane, could be multiple, so build the collection 

Thanks

Uma

0 Kudos
DrewD
by
New Contributor III

Thank you Uma, these are the results of the breakpoints below.

Here I'm using the variable PanelID to hold the _viewPaneID (which correctly matches my pane id in DAML).

Everything seems like it should be working, but the pane stays open

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Drew

You are correct, all the breakpoints seem OK.  I am attaching a simple Add-in with a pane and a button. If you run this sample, you will see a Close Pane button. Please check if this sample works.

Thanks

Uma