Visual Studio のダイアログを作成する

ダイアログは、ユーザーに情報の入力を求めたり、機能の動作をカスタマイズしたりするための方法です。 たとえば、[ツール/オプション] ダイアログには、テーマ、エディター、ドキュメント タブなどの機能の動作をユーザーが制御できる個々のページがあります。

作業の開始

作業を開始するには、「作業の開始」 セクションの「プロジェクト の作成」セクションに従います。

ダイアログを操作する

このガイドは、ダイアログを操作する際の上位のユーザー シナリオについて説明するように設計されています。

ダイアログを作成する

新しい機能拡張モデルを使用してツール ウィンドウを作成することは、ShellExtensibility ヘルパーから ShowDialogAsync メソッドを呼び出し、ダイアログ コンテンツを渡すのと同じくらい簡単です。

Screenshot of a Dialog.

ShowDialogAsync

ShowDialogAsync メソッドには、慣れ親しんだオーバーロードがいくつかあります。

Overloads

パラメーター

件名 タイプ 説明
コンテンツ Microsoft.VisualStudio.RpcContracts.RemoteUI.IRemoteUserControl ダイアログの内容。
タイトル System.String ダイアログのタイトル。
options 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を待つだけで、ユーザーが実行したアクションを表す a が返 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 サンプルを参照してください。