演练:调用 Visual Studio SDK 使用自动化

使用 Visual Studio 服务的此演练演示如何创建 Visual Studio 外接程序。 您创建的外接程序获取服务提供程序并使用它以获取服务。 可以使用此相同方法获取所有已提供的 Visual Studio 服务。 有关服务和服务提供程序的更多信息,请参见 服务。 下面的过程演示如何创建外接程序从外接程序中的服务。

创建外接程序

在本节中,将创建一个 Visual Studio 外接程序使用 Visual Studio 外接程序项目模板。

创建外接程序

  1. 打开 Visual Studio。 在 文件 菜单上,指向 然后单击 新项目

    此时将出现**“新建项目”**对话框。

  2. 新项目 对话框的左侧窗格中,展开节点 其他项目类型 然后单击 扩展性 节点。

  3. 创建名为 外接程序的新 Visual Studio 外接程序项目。

    Visual Studio 外接程序向导运行。

  4. 选择一种编程语言 页上,选择 使用 Visual C#,创建外接程序使用 Visual Basic,创建外接程序

  5. 选择一个应用程序宿主 页上,选择 Microsoft Visual Studio 2010 并清除的 Microsoft Visual Studio 2010 宏

  6. 输入名称和说明 页上,键入 " 名称 框中 声明 框中 MyAddin 和 MyAddin 演练 。

  7. 选择外接程序选项 页上,选择 您是否为命令禁止外接程序的 UI?。 清除其他复选框。

  8. 接受其他默认值。

  9. 生成解决方案并验证它在编译时不会出错。

从外接程序中的服务

以下步骤通过获取从外接程序中的服务引导您完成。

获取服务

  1. 打开文件 Connect.cs 或 Connect.vb 并将这些行。 using (c#) 或 Imports (Visual Basic) 语句:

    Imports System.Runtime.InteropServices
    Imports Microsoft.VisualStudio.OLE.Interop
    Imports Microsoft.VisualStudio.Shell.Interop
    Imports IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider
    
    using System.Runtime.InteropServices;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.Shell.Interop;
    using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
    
  2. 右击 " 解决方案资源管理器 的项目节点并将这些 .NET 引用:

    Microsoft.VisualStudio.OLE.Interop
    Microsoft.VisualStudio.Shell.Interop
    
  3. 添加这些代码行。 Exec 方法的 if(commandName == "Addin.Connect.Addin") 或 If commandName = "Addin.Connect.Addin" Then 子句:

    If commandName = "Addin.Connect.Addin" Then 
        Dim sp As IOleServiceProvider = DirectCast(_applicationObject, IOleServiceProvider)
        Dim SID As Guid = GetType(SVsUIShell).GUID
        Dim IID As Guid = GetType(IVsUIShell).GUID
        Dim output As IntPtr
        sp.QueryService(SID, IID, output)
        Dim uiShell As IVsUIShell = DirectCast(Marshal.GetObjectForIUnknown(output), IVsUIShell)
    
        Dim clsid As Guid = Guid.Empty
        Dim result As Integer
        uiShell.ShowMessageBox(0, clsid, "MyAddin", String.Format(System.Globalization.CultureInfo.CurrentCulture, "Inside " & Me.ToString()), String.Empty, 0, _
        OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_INFO, 0, result)
    
        handled = True 
        Return 
    End If
    
    if (commandName == "Addin.Connect.Addin")
    {
        IOleServiceProvider sp = (IOleServiceProvider)
          _applicationObject;
        Guid SID = typeof(SVsUIShell).GUID;
        Guid IID = typeof(IVsUIShell).GUID;
        IntPtr output;
        sp.QueryService(ref SID, ref IID, out output);
        IVsUIShell uiShell = (IVsUIShell)Marshal.GetObjectForIUnknown(output);
    
        Guid clsid = Guid.Empty;
        int result;
        uiShell.ShowMessageBox(
           0,
           ref clsid,
           "MyAddin",
           string.Format(
              CultureInfo.CurrentCulture, "Inside " + this.ToString()),
           string.Empty,
           0,
           OLEMSGBUTTON.OLEMSGBUTTON_OK,
           OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
           OLEMSGICON.OLEMSGICON_INFO,
           0,
           out result);
    
        handled = true;
        return;
    }
    

    此代码将当前应用程序对象 (类型 DTE2)。 IOleServiceProvider,然后调用 QueryService 获取 SVsUIShell 服务。 此服务提供一 IVsUIShell 接口。 ,当外接程序运行时, ShowMessageBox 方法显示消息框。

  4. 安装并启动外接程序项目按 F5 调试模式。

    这将启动 Visual Studio另一个实例。

  5. 在新 Visual Studio 实例中,在 工具 菜单上,单击 外接程序。 消息框显示此:

    MyAddin
    Inside Addin.Connect
    

请参见

任务

如何:停用并移除外接程序