Get reference of button from Module

454
2
Jump to solution
03-16-2021 08:15 PM
PrashantKirpan
Occasional Contributor

Hi,

I've added simple button in my Addin and executing some business functionalities by clicking on button.

I'm capturing ProjectOpenedEvent  in Module1.cs and want to execute come code located in button class. 

No dockpanel associated with button so can't use FrameworkApplication.DockPaneManager.Find.

I want to call public function defined in button from Module1.cs but not sure how to take reference of button data context.

Any help would be appreciated.

-Prashant

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

As best practice the SDK team usually recommends to add common code to the Module class and then call the common code from buttons, tools etc. as shown in this example:  https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/19a330dcbe57cdbbde7f0762e9321aa7b411c8... 

The GetPlugInWrapper method shown in the reply above only gives you access to the ICommand interface but doesn't give you access to the underlying classes.  You also have to consider the just-in-time (JIT) strategy that is used for all controls in ArcGIS Pro: By default, controls appear enabled, but are not actually instantiated until they are clicked. This just-in-time (JIT) strategy improves resource utilization and startup time by deferring the instantiation of controls until they are initiated by the end user, however, this can be a problem if you try to share common code that is part of an instance of class (for example a Button or MapTool class).  We recommend the Module class singleton for these common code patterns.  You can add them either as 'static' methods (or properties) or add them to the Module class instance.  The Module class instance provides an accessor out-of-box with this property: public static Module1 Current.  You can then use Module1.Current to get to the instance.

View solution in original post

0 Kudos
2 Replies
DHuantes
New Contributor III

Your Button will have an id in the <button> tag found in the Config.daml.   If you want to do the equivalent of clicking the button programmatically.  You need the following code.  Hope that helps.

var iCommand = FrameworkApplication.GetPlugInWrapper("<Your Button's ID Goes Here>") as ICommand;
if (iCommand != null && iCommand.CanExecute(null))
    iCommand.Execute(null);

 

Wolf
by Esri Regular Contributor
Esri Regular Contributor

As best practice the SDK team usually recommends to add common code to the Module class and then call the common code from buttons, tools etc. as shown in this example:  https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/19a330dcbe57cdbbde7f0762e9321aa7b411c8... 

The GetPlugInWrapper method shown in the reply above only gives you access to the ICommand interface but doesn't give you access to the underlying classes.  You also have to consider the just-in-time (JIT) strategy that is used for all controls in ArcGIS Pro: By default, controls appear enabled, but are not actually instantiated until they are clicked. This just-in-time (JIT) strategy improves resource utilization and startup time by deferring the instantiation of controls until they are initiated by the end user, however, this can be a problem if you try to share common code that is part of an instance of class (for example a Button or MapTool class).  We recommend the Module class singleton for these common code patterns.  You can add them either as 'static' methods (or properties) or add them to the Module class instance.  The Module class instance provides an accessor out-of-box with this property: public static Module1 Current.  You can then use Module1.Current to get to the instance.

View solution in original post

0 Kudos