2012-06-14 47 views
1

我已经有一段时间了现在这个问题,我找不到任何地方的解决方案。 我目前正在为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) 
         ); 

当我发出的“登录”命令,我已经成功登录了,我希望它周围的其他方法,使“登录”命令在菜单中被禁用,“注销”被启用 - 直到我注销。

这就是我卡住的地方。我只是不知道在哪里以及如何实现命令的状态切换。 我想我必须在QueryStatus()方法中处理这个问题,但是关于这个主题的微软文档没有什么帮助或者睁开眼睛。

回答

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

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) 
        ); 

} 

资源:

+0

谢谢,这确实帮助了我,但我实际上的意思是**如何**实际更改命令的状态,以便它们启用或禁用。 – Christian

+0

请再检查一次。不知道它是否有效,我还没有测试过。 – Schaliasos

+0

我已经实现了'LoginEvent_AfterExecute()'方法在这里发布,但我得到以下错误:_No重载'LoginEvent_AfterExecute'匹配委托'EnvDTE._dispCommandEvents_AfterExecuteEventHandler'_。 这个错误发生在这一点上: 'LoginEvent.AfterExecute + =新_dispCommandEvents_AfterExecuteEventHandler(LoginEvent_AfterExecute);' 我的不足我的C#知识遗憾。但我在互联网上找不到任何有用的东西。再次感谢你的帮助! – Christian