Creating a tool

Tools provide a simple way to surface logic in the ArcGIS Viewer for Silverlight that should be initiated by the user. If it makes sense to have the functionality you're implementing initiated by clicking a button on the toolbar, then you should encapsulate the functionality in a tool.

See Best Practices: Getting Started and Extending the ArcGIS Viewer for Silverlight for more information.

The Viewer allows surfacing objects that implement the ICommand interface as tools. This interface provides a few simple members. In the context of the Viewer, 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 Microsoft's Managed Extensibility Framework (MEF). This attribute informs the Viewer that the tool should be made available for adding to the toolbar. When you include it on a tool you implement, it will always take the following form:

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

The other required attribute to add is ESRI.ArcGIS.Client.Extensibility.DisplayNameAttribute. This determines the name of the tool as it appears to a designer when adding it to the Viewer. Additional optional attributes that you can specify from the ESRI.ArcGIS.Client.Extensibility assembly include CategoryAttribute, DefaultIconAttribute, and DescriptionAttribute. The attributes should be specified as follows:

[Export(typeof(ICommand))]
[DisplayName("Simple Command")]
[Category("My Tools")]
[Description("Simple tool with MessageBox")]
[DefaultIcon("Path to icon, ex: "/Viewer.Addins;component/Images/SimpleTool.png"")]
public class MyCommand : ICommand

An example of a simple command is shown below. The tool 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")]
[Category("My Tools")]
[Description("Simple tool with MessageBox")]
[DefaultIcon("Path to icon, ex: "/Viewer.Addins;component/Images/SimpleTool.png"")]
public class MyCommand : ICommand
{
     public void Execute(object parameter)
     {
          // Show a message box when the tool's button is clicked.
          MessageBox.Show("Simple tool executed");
     } 
 
     public bool CanExecute(object parameter)
     {
          // Show as executable (i.e., enable the button on the toolbar) unless the map is null.
          return MapApplication.Current.Map != null;
     }
 
     public event EventHandler CanExecuteChanged;
}

1/26/2015