向解决方案资源管理器上下文菜单添加两个命令按钮 - Visual Studio添加

时间:2012-03-21 16:04:41

标签: visual-studio add-in

当用户在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);   
        }
    }

1 个答案:

答案 0 :(得分:2)

我需要在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;
            }
        }
    }