Vensters van hulpprogramma's

Maak een dockable toolvenster met gegevensgebonden gebruikersinterface met behulp van Remote UI en de VisualStudio.Extensibility SDK.

Note

In deze handleiding wordt de VisualStudio.Extensibility SDK gebruikt. Zie Het juiste uitbreidbaarheidsmodel kiezen voor modelvergelijking.

Volledig voorbeeld

De volledige implementatie:

De definitie van het taakvenster:

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace ToolWindowSample;

using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Commands;
using Microsoft.VisualStudio.Extensibility.ToolWindows;
using Microsoft.VisualStudio.RpcContracts.RemoteUI;

/// <summary>
/// A sample tool window.
/// </summary>
[VisualStudioContribution]
public class MyToolWindow : ToolWindow
{
    private object? dataContext;

    /// <summary>
    /// Initializes a new instance of the <see cref="MyToolWindow" /> class.
    /// </summary>
    public MyToolWindow()
    {
        this.Title = "My Tool Window";
    }

    /// <inheritdoc />
    public override ToolWindowConfiguration ToolWindowConfiguration => new()
    {
        Placement = ToolWindowPlacement.DocumentWell,
        Toolbar = new ToolWindowToolbar(Toolbar),
    };

    [VisualStudioContribution]
    private static ToolbarConfiguration Toolbar => new("%ToolWindowSample.MyToolWindow.Toolbar.DisplayName%")
    {
        Children = [ToolbarChild.Command<MyToolbarCommand>()],
    };

    /// <inheritdoc />
    public override Task InitializeAsync(CancellationToken cancellationToken)
    {
        this.dataContext = new MyToolWindowData(this.Extensibility);

        return Task.CompletedTask;
    }

    /// <inheritdoc />
    public override Task<IRemoteUserControl> GetContentAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult<IRemoteUserControl>(new MyToolWindowControl(this.dataContext));
    }
}

De opdracht voor het openen van het taakvenster:

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace ToolWindowSample;

using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Commands;

/// <summary>
/// A sample command for showing a tool window.
/// </summary>
[VisualStudioContribution]
public class MyToolWindowCommand : Command
{
    /// <inheritdoc />
    public override CommandConfiguration CommandConfiguration => new("%ToolWindowSample.MyToolWindowCommand.DisplayName%")
    {
        Placements = [CommandPlacement.KnownPlacements.ToolsMenu],
        Icon = new(ImageMoniker.KnownValues.ToolWindow, IconSettings.IconAndText),
    };

    /// <inheritdoc />
    public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
    {
        await this.Extensibility.Shell().ShowToolWindowAsync<MyToolWindow>(activate: true, cancellationToken);
    }
}

Tip

Bekijk het volledige werkende voorbeeld: ToolWindowSample ↗

Gerelateerde concepten:Remote UI — het gegevensbindingsmodel dat wordt gebruikt voor inhoud van hulpprogrammavensters, Bijdragen en configuraties voor hulpprogrammavensters — hoe bijdragen voor hulpprogrammavensters worden gedeclareerd

Begrijp het voorbeeld

Stap 1: De klasse hulpprogrammavenster definiëren

Breid ToolWindow uit en markeer het met [VisualStudioContribution]:

[VisualStudioContribution]
internal class MyToolWindow : ToolWindow
{
    private MyDataModel? dataModel;

    public MyToolWindow(VisualStudioExtensibility extensibility)
        : base(extensibility)
    {
        Title = "My Tool Window";
    }

    public override ToolWindowConfiguration ToolWindowConfiguration => new()
    {
        Placement = ToolWindowPlacement.DocumentWell,
        AllowAutoCreation = true,
    };

    public override async Task InitializeAsync(CancellationToken cancellationToken)
    {
        dataModel = new MyDataModel();
        await dataModel.LoadAsync(cancellationToken);
    }

    public override Task<IRemoteUserControl> GetContentAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult<IRemoteUserControl>(new MyToolWindowControl(dataModel!));
    }
}

Stap 2: Het gegevensmodel maken

Gebruik NotifyPropertyChangedObject met [DataContract] en [DataMember] voor externe UI-gegevensbinding:

[DataContract]
internal class MyDataModel : NotifyPropertyChangedObject
{
    private string message = "Hello from the tool window!";

    [DataMember]
    public string Message
    {
        get => message;
        set => SetProperty(ref message, value);
    }

    public async Task LoadAsync(CancellationToken cancellationToken)
    {
        // Load data asynchronously
    }
}

Stap 3: Het Remote UI-besturingselement maken

Voeg een RemoteUserControl klasse en een overeenkomende XAML-gegevenssjabloon toe.

MyToolWindowControl.cs:

internal class MyToolWindowControl : RemoteUserControl
{
    public MyToolWindowControl(MyDataModel dataModel)
        : base(dataContext: dataModel)
    {
    }
}

MyToolWindowControl.xaml : stel de buildactie in op EmbeddedResource (niet op pagina):

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:vs="http://schemas.microsoft.com/visualstudio/extensibility/2022/xaml">
    <StackPanel Margin="10">
        <TextBlock Text="{Binding Message}" FontSize="14" />
    </StackPanel>
</DataTemplate>

Important

Het XAML-bestand moet de EmbeddedResource build-actie gebruiken. Als u dit instelt op Page, wordt het taakvenster leeg weergegeven.

Stap 4: Een opdracht toevoegen om het taakvenster weer te geven

[VisualStudioContribution]
internal class ShowMyToolWindowCommand : Command
{
    public ShowMyToolWindowCommand(VisualStudioExtensibility extensibility)
        : base(extensibility)
    {
    }

    public override CommandConfiguration CommandConfiguration => new("My Tool Window")
    {
        Placements = [CommandPlacement.KnownPlacements.ViewOtherWindowsMenu],
        Icon = new(ImageMoniker.KnownValues.ToolWindow, IconSettings.IconAndText),
    };

    public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
    {
        await Extensibility.Shell().ShowToolWindowAsync<MyToolWindow>(activate: true, cancellationToken);
    }
}

Stap 5 (optioneel): Een werkbalk toevoegen

Definieer een werkbalk en verwijs ernaar in ToolWindowConfiguration:

[VisualStudioContribution]
public static ToolbarConfiguration MyToolbar => new("%MyToolbar.DisplayName%")
{
    Children = [
        ToolbarChild.Command<RefreshCommand>(),
        ToolbarChild.Separator,
        ToolbarChild.Command<ClearCommand>(),
    ],
};

public override ToolWindowConfiguration ToolWindowConfiguration => new()
{
    Placement = ToolWindowPlacement.DocumentWell,
    Toolbar = new(MyToolbar),
};

Tips

✅ DO

  • Markeer XAML-bestanden als de buildactie EmbeddedResource (niet Page).
  • Gebruik [DataContract] en [DataMember] op ViewModel-eigenschappen voor Remote UI.
  • Gebruik NotifyPropertyChangedObject als basis voor gegevensmodellen.
  • Initialiseer gegevens in InitializeAsync(), bouw de gebruikersinterface op in GetContentAsync().
  • Gebruik ShowToolWindowAsync<T>(activate: true) om vanuit opdrachten weer te geven.

❌ NIET DOEN

  • Gebruik WPF niet rechtstreeks — Remote UI is vereist voor out-of-process-extensies.
  • Stel XAML-buildactie niet in op Page (het taakvenster wordt niet weergegeven).
  • Maak geen dure bronnen in de constructor — gebruik InitializeAsync.

🔧 Problemen oplossen

Symptom Repareren
Het taakvenster is leeg Controleer of de XAML-buildactie EmbeddedResource is
Gegevensbinding wordt niet bijgewerkt Controleren of ViewModel erft van NotifyPropertyChangedObject en eigenschappen heeft [DataMember]
Venster wordt niet weergegeven Controleer of opdrachtaanroepen ShowToolWindowAsync correct werken
Werkbalk wordt niet weergegeven Controleer of ToolWindowConfiguration.Toolbar is ingesteld

Volgende stappen