命令選擇區提供完整的延伸模組模型,可讓開發人員為調色盤建立自己的體驗。
如需最 up-to日期範例,請參閱 GitHub 上的範例專案。 這其中包含命令調色板所有功能的範例,以及 up-to日期範例。
建立命令以執行某些動作
建立實作 IInvokableCommand 並 實作 Invoke 方法的類別。 當使用者在命令選擇區中選取命令時,將會呼叫這個方法。
class MyCommand : Microsoft.CommandPalette.Extensions.Toolkit.InvokableCommand {
public class MyCommand()
{
Name = "Do it"; // A short name for the command
Icon = new("\uE945"); // Segoe UI LightningBolt
}
// Open GitHub in the user's default web browser
public ICommandResult Invoke() {
Process.Start(new ProcessStartInfo("https://github.com") { UseShellExecute = true });
// Hides the Command Palette window, without changing the page that's open
return CommandResult.Hide();
}
}
建立命令頁面
下列範例示範如何建立命令的頁面。 當使用者在命令選擇區中選取 [開啟] 命令時,就會顯示此頁面:
using Microsoft.CommandPalette.Extensions.Toolkit;
class MyPage : ListPage {
public MyPage()
{
Icon = IconHelpers.FromRelativePath("Assets\\StoreLogo.png");
Title = "My sample extension";
Name = "Open";
}
public override IListItem[] GetItems()
{
return [
new ListItem(new OpenUrlCommand("https://github.com"))
{
Title = "Open GitHub",
},
new ListItem(new OpenUrlCommand("https://learn.microsoft.com"))
{
Title = "Open Microsoft Learn",
},
new ListItem(new OpenUrlCommand("https://github.com/microsoft/PowerToys"))
{
Title = "Open PowerToys on GitHub",
},
new ListItem(new CopyTextCommand("Foo bar"))
{
Title = "Copy 'Foo bar' to the clipboard",
},
];
}
}
圖示
您可以使用 IIconInfo 類別的圖示,以多種方式指定。 以下是一些範例:
using Microsoft.CommandPalette.Extensions.Toolkit;
namespace ExtensionName;
internal sealed class Icons
{
// Icons can be specified as a Segoe Fluent icon, ...
internal static IconInfo OpenFile { get; } = new("\uE8E5"); // OpenFile
// ... or as an emoji, ...
internal static IconInfo OpenFileEmoji { get; } = new("📂");
// ... Or as a path to an image file, ...
internal static IconInfo FileExplorer { get; } = IconHelpers.FromRelativePath("Assets\\FileExplorer.png");
// ... which can be on a remote server, or svg's, or ...
internal static IconInfo FileExplorerSvg { get; } = new("https://raw.githubusercontent.com/microsoft/PowerToys/refs/heads/main/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.Indexer/Assets/FileExplorer.svg");
// Or they can be embedded in a exe / dll:
internal static IconInfo FolderIcon { get; } = new("%systemroot%\\system32\\system32\\shell32.dll,3");
}