创建 Visual Studio 对话框

对话是提示用户提供信息或允许自定义功能行为的一种方法。 例如, “工具>选项 ”对话框包含单个页面,允许用户控制主题、编辑器和文档选项卡等功能的行为。

开始吧

若要开始,请按照 创建第一个 Visual Studio 扩展中的步骤作。

使用对话

在处理对话框时,本文涵盖了主要用户场景:

创建对话框

使用新的扩展性模型创建工具窗口非常简单,只需从 ShellExtensibility 帮助程序调用 ShowDialogAsync 方法并传入对话内容即可。

显示对话框的屏幕截图。

ShowDialogAsync

ShowDialogAsync 方法有多个重载,你应该熟悉这些重载:

重载

参数

名称 类型 DESCRIPTION
content Microsoft.VisualStudio.RpcContracts.RemoteUI.IRemoteUserControl 对话框的内容。
title System.String 对话框的标题。
options Microsoft.VisualStudio.RpcContracts.Notifications.DialogOption 用于显示对话框的选项。
cancellationToken System.Threading.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