根据执行的命令启用/禁用命令

时间:2012-06-14 10:05:09

标签: c# visual-studio visual-studio-addins

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

我通过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)
                        );

当我发出“login”命令并且我已成功登录时,我想要反过来,以便在菜单中禁用“login”命令并启用“logout” - 直到我退出。

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

2 个答案:

答案 0 :(得分: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)
                    );

}

资源:

答案 1 :(得分: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的帮助。我很乐意把你的答案投票给我,但是因为我落后于声望点,所以我不能。