次の方法で共有


拡張機能のサンプル

コマンド パレットには完全な拡張モデルが用意されており、開発者はパレット用に独自のエクスペリエンスを作成できます。

最も up-to-date サンプルについては、 GitHub のサンプル プロジェクトを参照してください。 これには、コマンド パレットで可能なすべての up-to-date サンプルが含まれています。

何かを行うコマンドを作成する

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");
}