Skip to content

Commit 3d45038

Browse files
Updates
1 parent 86b0eed commit 3d45038

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"cSpell.words": [
3+
"Cascadia",
34
"VSCT",
45
"VSIX",
56
"VSSDK",

docs/assets/css/style.scss

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pre {
8484
margin: -1em 0 2em 0;
8585
font-size: .8em;
8686
width: 100%;
87+
line-height: 1.4;
8788
}
8889

8990
code {
@@ -99,15 +100,17 @@ code .s {
99100
color: var(--code-s);
100101
}
101102

102-
code .n {
103+
code .n,
104+
code .na {
103105
color: var(--code-n);
104106
}
105107

106108
code .k {
107109
color: var(--code-k);
108110
}
109111

110-
code .nf {
112+
code .nf,
113+
code .nt {
111114
color: var(--code-nf);
112115
}
113116

@@ -210,6 +213,16 @@ main .meta {
210213
right: 0
211214
}
212215

216+
main > section ol,
217+
main > section ul {
218+
padding-left: 2.5em;
219+
margin-bottom: 1em;
220+
}
221+
222+
main > section li::marker {
223+
font-weight: bold;
224+
}
225+
213226
footer {
214227
padding: 1em 0;
215228
text-align: center;
Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,56 @@
11
---
2-
title: Menus and commands
2+
title: Menus & commands
33
date: 2021-5-24
44
---
55

6-
TBD
6+
Commands are most often used as buttons in menus around Visual Studio. To create a command requires two steps:
7+
8+
1. Define the command
9+
2. Handle the click/invocation
10+
11+
## Define the command
12+
Every button in every menu is a command. To add a command to your extension, you must define it in the .vsct file first. It could look something like this:
13+
14+
```xml
15+
<Buttons>
16+
<Button guid="MyPackage" id="MyCommand" priority="0x0105" type="Button">
17+
<Parent guid="VSMainMenu" id="View.DevWindowsGroup.OtherWindows.Group1"/>
18+
<Icon guid="ImageCatalogGuid" id="StatusInformation" />
19+
<CommandFlag>IconIsMoniker</CommandFlag>
20+
<Strings>
21+
<ButtonText>R&amp;unner Window</ButtonText>
22+
</Strings>
23+
</Button>
24+
</Buttons>
25+
```
26+
27+
This button is placed in the parent group located in the **View -> Other Windows** menu as specified in the `Parent` element.
28+
29+
You can now run the extension now to see if the command shows up in the right location and menu.
30+
31+
## Handle the click/invocations
32+
Once the button is defined, we need to handle what happens when it is invoked. We do that in a C# class that looks like this:
33+
34+
```csharp
35+
[Command("489ba882-f600-4c8b-89db-eb366a4ee3b3", 0x0100)]
36+
public class MyCommand : BaseCommand<TestCommand>
37+
{
38+
protected override Task ExecuteAsync(OleMenuCmdEventArgs e)
39+
{
40+
// Do something
41+
}
42+
}
43+
```
44+
45+
Make sure to call it from your `Package` class's `InitializeAsync` method.
46+
47+
```csharp
48+
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
49+
{
50+
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
51+
52+
await MyCommand.InitializeAsync(this);
53+
}
54+
```
55+
56+
The command Guid and ID must match the guid/id pair from `Button` element in the .vsct file

0 commit comments

Comments
 (0)