建立 Visual Studio 對話方塊

對話方塊是提示使用者提供資訊或允許自訂功能行為的方式。 例如,[工具/選項] 對話方塊有個別頁面,可讓使用者控制主題、編輯器和文件索引標籤等功能的行為。

開始使用

若要開始,請遵循《使用者入門》一節中的建立專案一節。

使用對話方塊

本指南介紹了使用對話方塊時的主要使用者案例:

建立對話方塊

使用新的 [擴充性模型] 建立工具視窗就像從 ShellExtensibility 協助程式呼叫 ShowDialogAsync 方法一樣簡單,並傳入對話內容。

Screenshot of a Dialog.

ShowDialogAsync

ShowDialogAsync 方法有數個您應該熟悉的多載:

Overloads

參數

姓名 類型​​ 描述
content Microsoft.VisualStudio.RpcContracts.RemoteUI.IRemoteUserControl 對話方塊內容。
title System.String 對話方塊標題。
電子商務選項中 Microsoft.VisualStudio.RpcContracts.Notifications.DialogOption 顯示對話方塊的選項。
cancellationToken System.Threading.CancellationToken CancellationToken 用來取消對話方塊。
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);
}

有關建立 RemoteUserControl 的詳細資訊,請參閱遠端 UI

自訂對話方塊標題

當您的擴充功能顯示對話方塊時,您可以提供會顯示在對話方塊標題區域的自訂標題字串。

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

自訂對話方塊按鈕

當 IDE 中顯示對話方塊時,可以選取預先定義對話方塊按鈕和預設動作的特定組合。 您可以在 Microsoft.VisualStudio.RpcContracts.Notifications.DialogOption 中找到預先定義的按鈕和動作組合。

Screenshot of a Dialog button.

此外,您可以從下列建立自己的按鈕和預設動作組合:

  • 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
    }
    

範例

新增取消按鈕:

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

沒有對話方塊按鈕:

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

取得對話方塊結果

如果您需要知道使用者是否已肯定地關閉對話方塊或將其取消,您可以等候對 ShowDialogAsync的呼叫,而且會傳回 Microsoft.VisualStudio.RpcContracts.Notifications.DialogResult,代表使用者所採取的動作。

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
	}
}

下一步

如需使用對話方塊建立擴充功能完整範例,請參閱 DialogSample 範例。