將功能表和命令新增至 Visual Studio 擴充功能
本文將逐步引導您完成將功能表和命令新增至 Visual Studio 擴充功能的步驟。 命令最常用來做為 Visual Studio 周圍功能表的按鈕。 建立命令需要兩個步驟:
- 定義命令
- 處理按一下/叫用
定義命令
每個功能表中的每個按鈕都是命令。 若要將命令新增至擴充功能,必須先在 .vsct 檔案中將其定義。 它可能看起來像這樣:
<Buttons>
<Button guid="MyPackage" id="MyCommand" priority="0x0105" type="Button">
<Parent guid="VSMainMenu" id="View.DevWindowsGroup.OtherWindows.Group1"/>
<Icon guid="ImageCatalogGuid" id="StatusInformation" />
<CommandFlag>IconIsMoniker</CommandFlag>
<Strings>
<ButtonText>R&unner Window</ButtonText>
</Strings>
</Button>
</Buttons>
此按鈕會放置在位於 Parent
項目所指定之檢視>其他視窗功能表的父群組中。
您現在可以立即執行擴充功能,以查看命令是否顯示在正確的位置和功能表中。
處理按一下/叫用
定義按鈕之後,我們需要處理叫用按鈕時會發生什麼事。 我們在 C# 類別中執行此動作,如下所示:
[Command("489ba882-f600-4c8b-89db-eb366a4ee3b3", 0x0100)]
public class MyCommand : BaseCommand<TestCommand>
{
protected override Task ExecuteAsync(OleMenuCmdEventArgs e)
{
// Do something
}
}
請務必從 Package
類別的 InitializeAsync
方法呼叫它。
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await this.RegisterCommandsAsync();
}
命令 Guid 和識別碼必須符合 .vsct 檔案中 Button
項目的 guid/id 組