Writing tools

Tools provide a simple way to expose logic in the ArcGIS Map Web Part that should be initiated by the user. If it makes sense to have the functionality you are implementing initiated by having a user click a button on the ribbon, you should encapsulate this functionality in a tool.

The Map Web Part exposes commands (tools) that implement the ICommand interface. This interface provides a few simple members. In the context of the Map Web Part, these members are used as follows:

In addition to implementing these members, you must also add two attributes to the class that implements ICommand. The first is System.ComponentModel.Composition.ExportAttribute, which is included in the System.ComponentModel.Composition assembly provided as part of the Microsoft Managed Extensibility Framework (MEF). This attribute informs the Map Web Part that the command should be made available to add to the ribbon. When you include it on a command you implement, it will always take the following form:

[Export(typeof(ICommand))]
public class MyCommand : ICommand

The other attribute to add is ESRI.ArcGIS.Client.Extensibility.DisplayNameAttribute. This determines the name of the command as it appears to a designer when adding it to the Map Web Part. This attribute should be specified as follows:

[Export(typeof(ICommand))]
[DisplayName("<Name to display>")]
public class MyCommand : ICommand

An example of a simple command is shown in the following code. The command displays a message box and is enabled when the map is not null. In this case, the CanExecuteChanged event is not used.

[Export(typeof(ICommand))]
[DisplayName("Simple Command")]
public class SimpleCommand : ICommand
{
     public void Execute(object parameter)
     {
          // Show a message box when the command's ribbon button is clicked.
          MessageBox.Show("Simple command executed");
     } 
 
     public bool CanExecute(object parameter)
     {
          // Show as executable (i.e., enable the button on the ribbon) unless the map is null.
          return MapApplication.Current.Map != null;
     }
 
     public event EventHandler CanExecuteChanged;
}

11/30/2012