Erstellen von Visual Studio-Dialogen

Dialogfelder sind eine Möglichkeit, einen Benutzer zur Eingabe von Informationen aufzufordern oder Anpassungen des Featureverhaltens zu ermöglichen. Beispielsweise verfügt das Dialogfeld "Extras/Optionen" über einzelne Seiten, mit denen der Benutzer das Verhalten für Features wie Designs, Editoren und Dokumentregisterkarten steuern kann.

Erste Schritte

Folgen Sie zunächst dem Abschnitt "Projekt erstellen" im Abschnitt "Erste Schritte".

Arbeiten mit Dialogen

Dieses Handbuch wurde entwickelt, um die wichtigsten Benutzerszenarien beim Arbeiten mit Dialogfeldern abzudecken:

Dialogfeld erstellen

Das Erstellen eines Toolfensters mit dem neuen Erweiterbarkeitsmodell ist so einfach wie das Aufrufen der ShowDialogAsync-Methode über die ShellExtensibility-Hilfsprogramme und das Übergeben von Dialoginhalten.

Screenshot of a Dialog.

ShowDialogAsync

Die ShowDialogAsync-Methode verfügt über mehrere Überladungen, mit denen Sie vertraut sein sollten:

Overloads

Parameter

Name Typ Beschreibung
content Microsoft.VisualStudio.RpcContracts.RemoteUI.IRemoteUserControl Der Inhalt des Dialogfelds.
title System.String Der Titel des Dialogfelds.
Optionen Microsoft.VisualStudio.RpcContracts.Notifications.DialogOption Die Optionen zum Anzeigen des Dialogfelds.
cancellationToken System.Threading.CancellationToken Ein CancellationToken zum Abbrechen des Dialogfelds.
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
	// Ownership of the RemoteUserControl is transferred to Visual Studio, so it shouldn't be disposed by the extension
	#pragma warning disable CA2000 // Dispose objects before losing scope
	var control = new MyDialogControl(null);
	#pragma warning restore CA2000 // Dispose objects before losing scope

	await this.Extensibility.Shell().ShowDialogAsync(control, cancellationToken);
}

Weitere Informationen zum Erstellen eines RemoteUserControl-Elements finden Sie unter Remote-UI.

Anpassen des Dialogfeldtitels

Wenn die Erweiterung ein Dialogfeld anzeigt, können Sie eine benutzerdefinierte Titelzeichenfolge angeben, die im bereich Untertitel des Dialogfelds angezeigt wird.

Screenshot showing a Dialog title.

public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
	// Ownership of the RemoteUserControl is transferred to Visual Studio, so it shouldn't be disposed by the extension
	#pragma warning disable CA2000 // Dispose objects before losing scope
	var control = new MyDialogControl(null);
	#pragma warning restore CA2000 // Dispose objects before losing scope

	await this.Extensibility.Shell().ShowDialogAsync(control, "My Dialog Title", cancellationToken);
}

Anpassen der Dialogfeldschaltflächen

Wenn ein Dialogfeld in der IDE angezeigt wird, können bestimmte Kombinationen vordefinierter Dialogfeldschaltflächen und Standardaktionen ausgewählt werden. Die vordefinierten Schaltflächen und Aktionskombinationen finden Sie in Microsoft.VisualStudio.RpcContracts.Notifications.DialogOption.

Screenshot of a Dialog button.

Darüber hinaus können Sie eine eigene Kombination aus Schaltflächen und Standardaktionen erstellen aus:

  • Microsoft.VisualStudio.RpcContracts.Notifications.DialogButton

      public enum DialogButton
      {
      	// Hides all of the dialog buttons.
      	None,
      	// Shows a single close button.
      	Close,
      	// Shows a single OK button.
      	OK,
      	// Shows an OK and Cancel button.
      	OKCancel
      }
    
  • Microsoft.VisualStudio.RpcContracts.Notifications.DialogResult

    public enum DialogResult
      {
      	// The dialog was closed via the System.Threading.CancellationToken or using an
      	// action provided by the Microsoft.Visual Studio.RpcContracts.RemoteUI.IRemoteUserControl
      	// content.
      	None,
      	// The user clicked the Close button.
      	Close,
      	// The user clicked the OK button.
      	OK,
      	// The user clicked the Cancel button, or clicked the nonclient close button, or
      	// pressed the Esc key.
      	Cancel
    }
    

Beispiele

Hinzufügen einer Schaltfläche "Abbrechen":

public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
	// Ownership of the RemoteUserControl is transferred to Visual Studio, so it shouldn't be disposed by the extension
	#pragma warning disable CA2000 // Dispose objects before losing scope
	var control = new MyDialogControl(null);
	#pragma warning restore CA2000 // Dispose objects before losing scope

	await this.Extensibility.Shell().ShowDialogAsync(control, DialogOption.OKCancel, cancellationToken);
}

Es gibt keine Dialogfeldschaltflächen:

public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
	// Ownership of the RemoteUserControl is transferred to Visual Studio, so it shouldn't be disposed by the extension
	#pragma warning disable CA2000 // Dispose objects before losing scope
	var control = new MyDialogControl(null);
	#pragma warning restore CA2000 // Dispose objects before losing scope

	await this.Extensibility.Shell().ShowDialogAsync(control, new DialogOption(DialogButton.None, DialogResult.None), cancellationToken);
}

Abrufen des Dialogfeldergebnisses

Wenn Sie wissen müssen, ob ein Benutzer ein Dialogfeld bestätigend geschlossen oder geschlossen hat, können Sie auf den Aufruf ShowDialogAsyncwarten und es wird ein Microsoft.VisualStudio.RpcContracts.Notifications.DialogResult, das die vom Benutzer ausgeführte Aktion darstellt, zurückgegeben.

public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
	// Ownership of the RemoteUserControl is transferred to Visual Studio, so it shouldn't be disposed by the extension
	#pragma warning disable CA2000 // Dispose objects before losing scope
	var control = new MyDialogControl(null);
	#pragma warning restore CA2000 // Dispose objects before losing scope

	DialogResult result = await this.Extensibility.Shell().ShowDialogAsync(control, "My Dialog Title", DialogOption.OKCancel, cancellationToken);

	if (result == DialogResult.OK)
	{
		// User clicked the OK button
	}
}

Nächste Schritte

Im DialogSample-Beispiel finden Sie ein vollständiges Beispiel zum Erstellen einer Erweiterung mit einem Dialogfeld.