【问题标题】:Add two Command Buttons to Solution Explorer Context Menu - Visual Studion Add In将两个命令按钮添加到解决方案资源管理器上下文菜单 - Visual Studio 插件
【发布时间】:2012-03-21 16:04:41
【问题描述】:

当用户在 Visual Studio 2010 解决方案资源管理器中单击“文件夹”时,以下代码应在用户右键单击“项目”时显示自定义上下文菜单项和不同的自定义上下文菜单项。

第一部分工作得很好 - 你好新菜单项耶! - 用户右键单击文件夹时的第二部分 - 不是 - 它始终显示标准的旧上下文菜单而不显示新菜单项。

知道我哪里出错了吗?创建两个Command对象就可以了吧?

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;

        if(connectMode == ext_ConnectMode.ext_cm_UISetup)
        {
            object []contextGUIDS = new object[] { };
            Commands2 commands = (Commands2)_applicationObject.Commands;
            CommandBars cBars = (CommandBars)_applicationObject.CommandBars;
            try
            {
                Command commandProjectSettings = commands.AddNamedCommand2(_addInInstance, "DavecProjectSchemaSettings", "Davec Project Settings", "Manages Database Project Settings", false, 1, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                Command commandAddSchemaUpdate = commands.AddNamedCommand2(_addInInstance, "DavecProjectUpdate", "Davec Update", "Updates Database Schema", false, 2, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

                CommandBar vsBarProject = cBars["Project"];
                CommandBar vsBarFolder = cBars["Folder"];

                commandProjectSettings.AddControl(vsBarProject, 1);
                commandAddSchemaUpdate.AddControl(vsBarFolder, 1);

            }
            catch(System.ArgumentException)
            {
                //ignore
            }

            _solutionEvents = _applicationObject.Events.SolutionEvents;
            _solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(SolutionEvents_Opened);

            _documentEvents = _applicationObject.Events.DocumentEvents;
            _documentEvents.DocumentSaved += new _dispDocumentEvents_DocumentSavedEventHandler(_documentEvents_DocumentSaved);   
        }
    }

【问题讨论】:

    标签: visual-studio add-in


    【解决方案1】:

    我需要在 QueryStatus 方法中包含新命令,该方法在命令的可用性更新时调用,即

        public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
        {
            if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
            {
                if (commandName == "Sappha.Davec.VSAddIn.Connect.DavecProjectSchemaSettings")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
    
                if (commandName == "Sappha.Davec.VSAddIn.Connect.DavecProjectUpdate")
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多