You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Previous**: [Update a list of commands](update-a-list-of-commands.md).
13
13
14
-
So far, you've only added commands to a single page within your extension. You can also add more commands directly to the top-level list of commands too.
14
+
So far, you've only added commands to a single page within your extension. You can also add more commands directly to the top-level list of commands too.
15
15
16
-
## Adding the top-level commands
17
16
18
-
To do that, head on over to the `<ExtensionName>CommandsProvider.cs` file. This file is where you'll add commands that should be shown at the top-level of the Command Palette. As you can see, there's currently only a single item there:
17
+
## Top-level commands
18
+
19
+
To add commands at the top level of the Command Palette, open the `<ExtensionName>CommandsProvider.cs` file. This is where you define the commands that will appear at the root level of the Command Palette.
20
+
21
+
Currently, the file contains only one command:
19
22
20
23
```csharp
21
24
public<ExtensionName>CommandsProvider()
@@ -33,24 +36,38 @@ public override ICommandItem[] TopLevelCommands()
33
36
}
34
37
```
35
38
36
-
This sample extension creates a list of commands when the extension is created and returns that list whenever it's asked for the top-level commands. This prevents the extension from re-creating the list of commands every time the top-level commands are requested. This is a performance optimization.
39
+
When the extension is created, it builds a list of commands and stores them in the `_commands` field. Every time the extension is asked for its top-level commands, it simply returns this prebuilt list. This approach avoids recreating the command list on each request, which improves performance.
37
40
38
-
If you want to add another command to the top-level list of commands, you can add another **CommandItem**:
41
+
## Add another toplevel command
39
42
40
-
```csharp
43
+
1. In Visual Studio, open `<ExtensionName>CommandsProvider.cs`
new CommandItem(new <ExtensionName>Page()) { Title = DisplayName },
47
-
newCommandItem(newShowMessageCommand()) { Title="Send a message" },
53
+
+ new CommandItem(new ShowMessageCommand()) { Title = "Send a message" },
48
54
];
49
55
}
50
56
```
51
57
58
+
> [!NOTE]
59
+
> `ShowMessageCommand()` section was created prior at [InvokableCommand Command](adding-commands#invokableCommand-command)
60
+
61
+
1. Deploy your extension
62
+
1. In command palette, `Reload`
63
+
64
+

65
+
52
66
There you have it. Now you can add additional top-level commands to your extension.
53
67
68
+
69
+
70
+
54
71
If you'd like to update the list of top-level commands dynamically, you can do so in the same way as you would update a list page. This can be useful for cases like an extension that might first require the user to log in, before showing certain commands. In that case, you can show the "log in" command at the top level initially. Then, once the user logs in successfully, you can update the list of top-level commands to include the commands that required authentication.
55
72
56
73
Once you've determined that you need to change the top level list, call [RaiseItemsChanged](./microsoft-commandpalette-extensions-toolkit/commandprovider_raiseitemschanged.md) on your **CommandProvider**. Command Palette will then request the top-level commands via **TopLevelCommands** again, and you can return the updated list.
Copy file name to clipboardExpand all lines: hub/powertoys/command-palette/adding-commands.md
+50-19Lines changed: 50 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ Now that you've created your extension, it's time to add some commands to it.
17
17
18
18
We can start by navigating to the `<ExtensionName>Page.cs` file. This file is the [ListPage](./microsoft-commandpalette-extensions-toolkit/listpage.md) that will be displayed when the user selects your extension. In there you should see:
@@ -32,9 +32,9 @@ public override IListItem[] GetItems()
32
32
}
33
33
```
34
34
35
-
Here you can see that we've set the icon for the page, the title, and the name that's shown at the top-level when you have the command selected. The **GetItems** method is where you'll return the list of commands that you want to show on this page. Right now, that's just returning a single command that does nothing. Let's instead try making that command open _this_ page in the user's default web browser.
35
+
Here you can see that we've set the icon for the page, the title, and the name that's shown at the top-level when you have the command selected. The `GetItems` method is where you'll return the list of commands that you want to show on this page. Right now, that's just returning a single command that does nothing. Let's instead try making that command open _this_ page in the user's default web browser.
36
36
37
-
We can change the implementation of **GetItems** to the following:
37
+
1. Update `GetItems` to the following:
38
38
39
39
```csharp
40
40
publicoverrideIListItem[] GetItems()
@@ -51,17 +51,17 @@ public override IListItem[] GetItems()
51
51
52
52
To update the extension in the Command Palette you need to:
53
53
54
-
1. Deploy your app
54
+
1. Deploy your extension
55
55
1. In the Command Palette, type the "reload" command to refresh the extensions in the palette
56
56
57
57

58
58
59
59
> [!NOTE]
60
60
> There are several reload options, make sure to select the **Reload Command Palette extensions**
61
61
62
-
1. Scroll down to your extension and press `enter`
62
+
1. Scroll down to your extension and press `Enter`
63
63
1. Press `enter` on `Open the Command Palette documentation`
64
-
1. You should see that the command now opens the Command Palette documentation
64
+
1. You should see that the command opens the Command Palette documentation
65
65
66
66
The **OpenUrlCommand** is a helper for opening a URL in the user's default web browser.
67
67
@@ -87,7 +87,7 @@ As your building your extension, you'll most likely want to debug it.
87
87
}
88
88
```
89
89
90
-
1. Deploy your app, wait until it's successful
90
+
1. Deploy your extension, wait until it's successful
91
91
1. Confirm You're in Debug Configuration
92
92
93
93
<details>
@@ -106,13 +106,22 @@ As your building your extension, you'll most likely want to debug it.
106
106
107
107

108
108
109
-
1. In the Command Palette, `reload`
109
+
1. In the Command Palette, run `reload`
110
110
1. Go to your extension and select `Open the Command Palette documentation`.
111
-
1. In Visual Studio's output window, you should see `Debug message from GetItems`
111
+
1. In Visual Studio's Output window, you should see `Debug message from GetItems`
112
112
113
113
## InvokableCommand Command
114
114
115
-
You can also implement an extension however you want. Let's instead make a new command, that shows a **MessageBox**. To do that, we need to create a new class that implements **IInvokableCommand**.
115
+
Let's continue building a new command, that shows a **MessageBox**. To do that, we need to create a new class that implements `InvokableCommand`.
116
+
117
+
1. In Visual Studio, Add a New Class File
118
+
- Keyboard Shortcut: Press Ctrl + Shift + A
119
+
- Or in the Solution Explorer, go to Project > Add New Item...
Now we can add this command to the list of commands in the `<ExtensionName>Page.cs` file:
141
150
151
+
1. In the `<ExtensionName>.cs`, update the `GetItems`:
152
+
142
153
```csharp
143
154
publicoverrideIListItem[] GetItems()
144
155
{
@@ -154,29 +165,46 @@ public override IListItem[] GetItems()
154
165
}
155
166
```
156
167
157
-
Deploy and reload, and presto - a command to show a message box!
168
+
1. Deploy your extension
169
+
1. In command palette, `Reload`
170
+
171
+
And presto - a command to show a message box!
158
172
159
173
> [!TIP]
160
174
> At about this point, you'll probably want to initialize a git repo / {other source control method of your choice} for your project. This will make it easier to track changes, and to share your extension with others.
161
175
>
162
176
> We recommend using GitHub, as it's easy to collaborate on your extension with others, and get feedback, and share it with the world.
163
177
164
-
## Adding more pages
178
+
## Types of Pages
165
179
166
180
So far, we've only worked with commands that "do something". However, you can also add commands that show additional pages within the Command Palette. There are basically two types of "Commands" in the Palette:
167
-
-**IInvokableCommand** - These are commands that *do something*.
168
-
-**IPage** - These are commands that *show something*.
169
181
170
-
Because **IPage** implementations are **ICommand**'s, you can use them anywhere you can use commands. This means you can add them to the top-level list of commands, or to a list of commands on a page, the context menu on an item, etc.
182
+
-`InvokableCommand` - These are commands that **do something**
183
+
-`IPage` - These are commands that **show something**
184
+
185
+
Because `IPage` implementations are **ICommand**'s, you can use them anywhere you can use commands. This means you can add them to the top-level list of commands, or to a list of commands on a page, the context menu on an item, etc.
171
186
172
187
There are two different kinds of pages you can show:
188
+
173
189
-[ListPage](./microsoft-commandpalette-extensions-toolkit/listpage.md) - This is a page that shows a list of commands. This is what we've been working with so far.
190
+
191
+

192
+
174
193
-[ContentPage](./microsoft-commandpalette-extensions-toolkit/contentpage.md) - This is a page that shows rich content to the user. This allows you to specify abstract content, and let Command Palette worry about rendering the content in a native experience. There are two different types of content supported so far:
175
194
-[Markdown content](./using-markdown-content.md) - This is content that's written in Markdown, and is rendered in the Command Palette. See [MarkdownContent](./microsoft-commandpalette-extensions-toolkit/markdowncontent.md) for details.
195
+
196
+

197
+
176
198
-[Form content](./using-form-pages.md) - This is content that shows a form to the user, and then returns the results of that form to the extension. These are powered by [Adaptive Cards](https://aka.ms/adaptive-cards) This is useful for getting user input, or displaying more complex layouts of information. See [FormContent](./microsoft-commandpalette-extensions-toolkit/formcontent.md) for details.
177
199
200
+

178
201
179
-
Start by adding a new page that shows a list of commands. Create a new class that implements **ListPage**:
202
+
## Add more commands
203
+
204
+
Start by adding a new page that shows a list of commands. Create a new class that implements **ListPage**.
205
+
206
+
1. In the `Pages` folder, create a new class called `MySecondPage`
Extensions are written in C#. The fastest way to get started writing extensions is from the Command Palette itself. Just run the "Create a new extension" command, fill out the fields to populate the template project, and you should be ready to start.
12
+
Extensions are written in C#. The fastest way to get started writing extensions is from the Command Palette itself.
13
+
14
+
1. Open Command Palette
15
+
1. Run the `Create a new extension` command
16
+
1. Fill out the fields to populate the template project, and you should be ready to start.
13
17
14
18
## Create a new extension
15
19
16
20
The form will ask you for the following information:
17
21
18
22
-**ExtensionName**: The name of your extension. This will be used as the name of the project and the name of the class that implements your commands. Make sure it's a valid C# class name - it shouldn't have any spaces or special characters, and should start with a capital letter. Reference in docs as `<ExtensionName>`.
19
-
-**Extension Display Name**: The name of your extension as it will appear in the Command Palette. This can be a more human-readable name.
20
-
-**Output Path**: The folder where the project will be created.
21
-
- The project will be created in a subdirectory of the path you provided.
23
+
-**Extension Display Name**: The name of your extension as it will appear in the Command Palette. This can be a more human-readable name.
24
+
-**Output Path**: The folder where the project will be created.
25
+
- The project will be created in a subdirectory of the path you provided.
22
26
- If this path doesn't exist, it will be created for you.
23
27
24
28

@@ -51,7 +55,18 @@ Once you submit the form, Command Palette will automatically generate the projec
51
55
52
56
(with `<ExtensionName>` replaced with the name you provided)
53
57
54
-
From here, you can immediately build the project and run it. Once your package is deployed and running, Command Palette will automatically discover your extension and load it into the palette.
58
+
You can deploy and run your extension:
59
+
60
+
- In Visual Studio, Deploy your extension
61
+
<details>
62
+
<summary>How to Deploy your extension</summary>
63
+
64
+
1. In the navigation bar, click on `Build`
65
+
1. Click on `Deploy <ExtensionName>`
66
+
67
+
</details>
68
+
69
+
Once your package is deployed and running, Command Palette will automatically discover your extension and load it into the palette.
55
70
56
71
> [!TIP]
57
72
> Make sure you _deploy_ your app! Just **build**ing your application won't update the package in the same way that deploying it will.
@@ -66,7 +81,9 @@ From here, you can immediately build the project and run it. Once your package i
66
81
> ```
67
82
> These files are used by WinAppSdk to deploy your app as a package. Without it, anyone who clones your repo won't be able to deploy your extension.
68
83
69
-
You should be able to see your extension in the Command Palette at the end of the list of commands. Entering that command should take you to the page for your command, and you should see a single command that says `TODO: Implement your extension here`.
84
+
1. In the Command Palette, scroll down the list of commands
85
+
1. Press `Enter` on your <ExtensionName>
86
+
1. You should see a single command that says `TODO: Implement your extension here`.
70
87
71
88

72
89
@@ -76,7 +93,6 @@ When you make changes to your extension, you can rebuild your project and deploy
76
93
77
94

78
95
79
-
80
96
### Next up: [Add commands to your extension](adding-commands.md)
0 commit comments