1

我已经有一段时间了这个问题,我无法在任何地方找到解决方案。我目前正在为 Visual Studio 2010 编写一个加载项(使用 C#)。我在 VS 菜单栏中添加了一个新菜单。在这个菜单中有几个命令,例如“login”和“logout”。我想强制执行的行为是两个命令都是可见的,但最初只会启用“登录”而最初禁用“注销”。

我通过 OnConnection() 方法中的以下代码实现了这一点:

    LoginCommand = applicationObject.Commands.AddNamedCommand(
                           addInInstance, 
                           LOGIN_NAME,
                           LOGIN_CAPTION,  
                           LOGIN_TOOLTIP, 
                           true, 59, 
                           ref contextUIGuids,
                           (int)(vsCommandStatus.vsCommandStatusSupported | 
                                vsCommandStatus.vsCommandStatusEnabled)
                        );

    LogoutCommand = applicationObject.Commands.AddNamedCommand(
                            addInInstance, 
                            LOGOUT_NAME,
                            LOGOUT_CAPTION, 
                            LOGOUT_TOOLTIP, 
                            true, 59, 
                            ref contextUIGuids,
                            (int)(vsCommandStatus.vsCommandStatusSupported)
                        );

当我发出“登录”命令并成功登录时,我希望它反过来,以便在菜单中禁用“登录”命令并启用“注销” - 直到我注销。

这就是我卡住的地方。我只是不知道在哪里以及如何准确地实现命令的状态切换。我想我必须在 QueryStatus() 方法中处理这个问题,但微软关于这个主题的文档不太有用或令人大开眼界。

4

2 回答 2

2

您需要将AfterExecute事件添加到您的LoginCommand事件中。在方法中添加以下内容OnConnection

Events events = (EnvDTE.Events) applicationObject.Events;
CommandEvents LoginEvent = events.get_CommandEvents(LoginCommand.Guid, LoginCommand.ID);
cmdEvent.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(LoginEvent_AfterExecute);

并创建LoginEvent_AfterExecute方法:

private void LoginEvent_AfterExecute(string guid, int id, object customIn, object customOut)
{
    //Delete the LoginCommand from the commands2 object and recreate it
    LoginCommand.Delete();

    LoginCommand = applicationObject.Commands.AddNamedCommand(
                       addInInstance, 
                       LOGIN_NAME,
                       LOGIN_CAPTION,  
                       LOGIN_TOOLTIP, 
                       true, 59, 
                       ref contextUIGuids,
                       (int)(vsCommandStatus.vsCommandStatusSupported)
                    );

    //Delete the LogoutCommand and recreate it
    LogoutCommand.Delete();  

    LogoutCommand = applicationObject.Commands.AddNamedCommand(
                        addInInstance, 
                        LOGOUT_NAME,
                        LOGOUT_CAPTION, 
                        LOGOUT_TOOLTIP, 
                        true, 59, 
                        ref contextUIGuids,
                        (int)(vsCommandStatus.vsCommandStatusSupported| 
                            vsCommandStatus.vsCommandStatusEnabled)
                    );

}

资源:

于 2012-06-14T12:02:58.867 回答
1

好吧,我想出了一个解决方案,虽然我不太确定它是否优雅。执行命令(例如LoginCommand)后,该QueryStatus()方法会被多次调用,但 . 的值不同commandName

public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
    {
        if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
        {
            if (loginOkay == 0)
            {
                if (commandName == addInInstance.ProgID + "." + LOGIN_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported;
                }
                if (commandName == addInInstance.ProgID + "." + LOGOUT_NAME ||
                    commandName == addInInstance.ProgID + "." + LOCK_NAME ||
                    commandName == addInInstance.ProgID + "." + UNLOCK_NAME ||
                    commandName == addInInstance.ProgID + "." + CHECKIN_NAME ||
                    commandName == addInInstance.ProgID + "." + CHECKOUT_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                }
            }
            else if (loginOkay == 1)
            {
                if (commandName == addInInstance.ProgID + "." + LOGIN_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                }
                if (commandName == addInInstance.ProgID + "." + LOGOUT_NAME ||
                    commandName == addInInstance.ProgID + "." + LOCK_NAME ||
                    commandName == addInInstance.ProgID + "." + UNLOCK_NAME ||
                    commandName == addInInstance.ProgID + "." + CHECKIN_NAME ||
                    commandName == addInInstance.ProgID + "." + CHECKOUT_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported;
                }
            }
            else
            {
                status = vsCommandStatus.vsCommandStatusUnsupported;
            }
        }
    }

无论如何,感谢Schaliasos的帮助。我很乐意为您的答案投票,但由于我落后于声誉点,所以我不能。

于 2012-06-15T11:05:00.010 回答