Smart Space 11.1.1: Adding Custom Commands to Gadgets |
Note: This article assumes basic knowledge of how to create a Smart Space gadget. For an introduction to gadget creation, please see
Creating Your Smart Space First Gadget.
Overview
The gadget class provides access to menus and to the toolbar through its Services.Appearance property, which returns an object that implements the IAppearance interface. IAppearance exposes three properties Menu,
ContextMenu, and Toolbar. The properties represent, respectively, the custom menu, the context menu, and the toolbar. Each of the three properties returns an object that implements the
ICommandStrip interface, which in turn provides AddCommand,
InsertCommand, and RemoveCommand methods that can be used to add or remove custom commands.
The following example demonstrates how to add a command, add a submenu to the command,
and trigger code to run when the command is executed. Command creation and initialization code is placed in the overridden Init() method, which is the preferred location for gadget initialization.
- Adding a command. Before a command can be added,
an object that implements the
ICommand interface must be created. For maximum flexibility, a custom implementation may be provided. The Smart Space SDK provides a default implementation of ICommand, which can be accessed through one of the
Services.CommandFactory.CreateCommand methods.
- Adding submenus. Each command is uniquely identified by an ID, which is assigned to a command during creation. In the example below,
FileCommand is the ID assigned to the File command. IDs can be used in the overloaded AddCommand method to specify a submenu to which the command is added.
- Defining command functionality. To make the commands useful, an event handler can be assigned to the
Executed event of a command. This code will run each time the command is executed.
' Define the command variables.
' WithEvents keyword is used to specify that the objects will be used to handle events.
Private WithEvents fileCommand As ICommand
Private WithEvents openCommand As ICommand
Public Overrides Sub Init()
' Create the command objects, using default implementation of ICommand.
Dim fileCommand As ICommand = _
Services.CommandFactory.CreateCommand("fileCommand", "File", "File Commands")
openCommand = _
Services.CommandFactory.CreateCommand("openCommand", "Open", "Open File")
' Add "File" command to the custom menu.
Me.Services.Appearance.Menu.AddCommand(fileCommand)
' Add "Open" command to the sub-menu under "File" command.
Me.Services.Appearance.Menu.AddCommand(openCommand, "FileCommand")
End Sub
' This method will be executed each time the Open command is clicked.
Public Sub openCommand_Executed(ByVal sender As Object, ByVal e As EventArgs) Handles fileCommand.Executed
' Display a toast message.
Me.Services.ToastManager.Show("Please specify the file name.")
End Sub |
public override void Init()
{
// Create the command objects, using default implementation of ICommand.
ICommand fileCommand =
Services.CommandFactory.CreateCommand("fileCommand", "File", "File Commands");
ICommand openCommand =
Services.CommandFactory.CreateCommand("openCommand", "Open", "Open File");
// Add an event handler to be run when the Open command is executed.
openCommand.Executed += new System.EventHandler(openCommand_Executed);
// Add "File" command to the custom menu.
this.Services.Appearance.Menu.AddCommand(fileCommand);
// Add "Open" command to the sub-menu under "File" command.
this.Services.Appearance.Menu.AddCommand(openCommand, "fileCommand");
}
// This method will be executed each time the Open command is clicked.
public void openCommand_Executed(object sender, EventArgs e)
{
// Display a toast message.
this.Services.ToastManager.Show("Please specify the file name.");
}
|
|