【问题标题】:Visual Studio 2010 AddIn DTE2 not workingVisual Studio 2010 插件 DTE2 不工作
【发布时间】:2012-09-09 01:32:52
【问题描述】:

我正在尝试在 Visual Studio 2010 中创建一个插件,如下所示:

public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    EnvDTE80.Windows2 wins2obj;
    AddIn addinobj;
    object ctlobj = null;
    Window newWinobj;

    // A toolwindow must be connected to an add-in, so this line 
    // references one.
    addinobj = _applicationObject.AddIns.Item(1);
    wins2obj = (Windows2)_applicationObject.Windows;

    // This section specifies the path and class name of the windows 
    // control that you want to host in the new tool window, as well as 
    // its caption and a unique GUID.
    string assemblypath = "C:\\temp\\WindowsControlLibrary1.dll";
    string classname = "WindowsControlLibrary1.UserControl1";
    string guidpos = "{426E8D27-3D33-4FC8-B3E9-9883AADC679F}";
    string caption = "CreateToolWindow2 Test";

    // Create the new tool window and insert the user control in it.
    newWinobj = wins2obj.CreateToolWindow2(addinobj, assemblypath, 
      classname, caption, guidpos, ref ctlobj);
    newWinobj.Visible = true;
}

现在我需要将 DTE2 传递给窗口内新创建的对象 (ctlobj)。如果我在 ctlobj 中声明一个公共变量并在此处设置它,Visual Studio 会崩溃并出现此异常:

COM Exception was unhandled
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

。有什么想法???

谢谢!!

【问题讨论】:

    标签: visual-studio-2010 visual-studio-addins


    【解决方案1】:

    不过有一个解决方法:

    // Get an instance of the currently running Visual Studio IDE.
    EnvDTE80.DTE2 dte2;
    dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
    GetActiveObject("VisualStudio.DTE.10.0");
    

    【讨论】:

      【解决方案2】:

      我知道你从纳扎夫那里得到了答案,还有另一种方法,它更干净一点。使 dte 对象成为您的插件 class 中的公共属性,并将插件类传递给 newWinObj

      然后您的newWinObj 将可以访问 addin 和 dte 对象。好用,我用这个系统。

      【讨论】:

        猜你喜欢
        • 2011-04-17
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 2011-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-27
        相关资源
        最近更新 更多