Passing parameters to Icommand

1130
6
Jump to solution
03-16-2023 10:55 AM
Cheetah24
New Contributor III

Hi

How can I pass a parameter using the following code where commandId is a custom class that extends MapTool. Is CanExecute implemented to accept parameters? I always get false when I specify an object.

 

IPlugInWrapper wrapper = FrameworkApplication.GetPlugInWrapper(commandId);
var command = wrapper as ICommand; 

if ((command != null) && command.CanExecute(null))
  command.Execute(null);

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

You can use your module for parameter exchanging. Make property in module with public get and set methods. Before Execute call set parameter, and on tool activating read it.

View solution in original post

0 Kudos
6 Replies
GKmieliauskas
Esri Regular Contributor
0 Kudos
Cheetah24
New Contributor III

Thanks for the response, this example uses binding and don't think it's translates to what I'm trying to do with Maptool.

0 Kudos
GKmieliauskas
Esri Regular Contributor

You can use your module for parameter exchanging. Make property in module with public get and set methods. Before Execute call set parameter, and on tool activating read it.

0 Kudos
RichardDaniels
Regular Contributor

if your always getting null, you should check that you have Activated the tab your button is on. 

FrameworkApplication.ActivateTab('esri_mapping_homeTab');

One activated you execute the button with command..Execute() as you show.

0 Kudos
Cheetah24
New Contributor III

Hi. I only get false if i attempt to pass a parameter to canexecute()

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Neither classes derived from Button (processing your OnClick) nor classes derived from MapTool (processing your Tool) are accepting parameters.  If you need to configure any of these button or tool classes, i recommend using properties in your Module derived class (or settings if that's appropriate).  Your module derived class is a singleton and easily accessible from any of your code.

internal class Module1 : Module
{
    private static Module1 _this = null;
...
    internal static string MyToolSetting
    {
      get; set;
    }
}

To access this from your tool (within the same project):

internal class MapTool1 : MapTool
{
  private string myParameter;
  public MapTool1()
  {
    IsSketchTool = true;
    SketchType = SketchGeometryType.Rectangle;
    SketchOutputMode = SketchOutputMode.Map;
  }
  protected override Task OnToolActivateAsync(bool active)
  {
    myParameter = Module1.MyToolSetting;
    return base.OnToolActivateAsync(active);
  }
...
}
0 Kudos