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:
- Execute—Called when the button on the toolbar corresponding to the tool is clicked.
- CanExecute—Called frequently by the Viewer to check whether the tool is in a state where it can be executed. When this method returns true, the corresponding button on the toolbar will be enabled. When the method returns false, the button will be disabled.
- CanExecuteChanged—Event that can be raised when the executability of the command changes. When this event is raised, the Viewer will invoke the CanExecute method, allowing the add-in to update the state of the tool's button on the toolbar.
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;
}