Objective
In the previous two essays, I introduced the add-in operating mechanism, so that there is a basic understanding of the add-in events, lifecycles, and how to interact with vs. Now it's time to see how to do something with VS, which is what add-in development is all about.
In general, Add-in should provide some interface elements so that the user can do something, such as adding a menu item to the main menu, or adding a menu item to the context menu of the editor, in this article to see how to implement these.
About Commands (command)
Consider a very common scenario: in the process of writing code, select a piece of text, click Edit->copy (or toolbar button) or press CTRL + C, we can copy the selected text to the Clipboard, what happened behind the process?
What is the complete copy operation? The answer is an order. You can think of a command as a specific feature, such as copy, Paste, cut, and so on. VS itself is built with a large number of commands (thousands of), and the menu items, toolbars, or shortcuts mentioned above execute these commands. The Tools->options menu allows you to view a list of commands:
Figure 1:vs List of commands
It is noteworthy that execution commands do not have to pass through interface elements, such as in a command window:
Figure 2: Executing commands in command window
This also allows you to execute the same command. Now we know that there are several ways to execute a command, a menu, a toolbar, a shortcut key, or a command window, which is collectively called the trigger. In VS, the trigger is detached from the command: The user executes the command by the trigger, and the command is responsible for checking its state (name, visibility, availability, etc.) and executing it. This means that the command can correspond to one or more menu items, or not to any menu item.
On the other hand, there may still be a different situation for the same command, such as Edit.copy. The edit.copy command executes differently within a text editor and in Solution Manager. Here is the concept of command target, VS, which turns the command to the command target, and the command target executes the command in its own implementation. The conclusion is:
Object |
Duty |
Triggered by |
Provides a way for users to use |
Command |
A logical entity that checks its own state, can execute commands, or it can turn to a command target |
Command target |
Execute it on your own implementation according to the command passed over |