ダイアログは、ユーザーに情報の入力を求めたり、機能の動作をカスタマイズしたりするための方法です。 たとえば、[ ツール>オプション ] ダイアログには、テーマ、エディター、ドキュメント タブなどの機能の動作をユーザーが制御できる個々のページがあります。
始めましょう
開始するには、「 最初の Visual Studio 拡張機能を作成する」の手順に従います。
ダイアログの操作
この記事では、ダイアログを操作するときの上位のユーザー シナリオについて説明します。
ダイアログを作成する
新しい機能拡張モデルを使用してツール ウィンドウを作成することは、ShellExtensibility ヘルパーから ShowDialogAsync メソッドを呼び出し、ダイアログ コンテンツを渡すのと同じくらい簡単です。
ShowDialogAsync
ShowDialogAsync メソッドには、習得すべきオーバーロードがいくつかあります。
オーバーロード
ShowDialogAsync(content,cancellationToken)
ShowDialogAsync(content,title,cancellationToken)
ShowDialogAsync(content,options,cancellationToken)
ShowDialogAsync(content,title,options,cancellationToken)
パラメーター
名前 | タイプ | 説明 |
---|---|---|
content |
Microsoft.VisualStudio.RpcContracts.RemoteUI.IRemoteUserControl | ダイアログの内容。 |
title |
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);
}
リモート ユーザー コントロールを作成する方法の詳細については、「 リモート UI」を参照してください。
ダイアログ のタイトルをカスタマイズする
拡張機能にダイアログが表示されたら、ダイアログのキャプション領域に表示されるカスタム タイトル文字列を指定できます。
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);
}
ダイアログ ボタンをカスタマイズする
統合開発環境にダイアログが表示されると、定義済みのダイアログ ボタンと既定のアクションの特定の組み合わせを選択できます。 定義済みのボタンとアクションの組み合わせは、 Microsoft.VisualStudio.RpcContracts.Notifications.DialogOption
にあります。
ボタンと既定のアクションの独自の組み合わせを作成することもできます。
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 by using an // action provided by the Microsoft.Visual Studio.RpcContracts.RemoteUI.IRemoteUserControl // content. None, // The user pressed the Close button. Close, // The user pressed the OK button. OK, // The user pressed the Cancel button, or pressed 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)
{
// The user pressed the OK button.
}
}
関連コンテンツ
- ダイアログで拡張機能を作成する方法の完全な例については、 DialogSample を参照してください。