根据执行的命令启用/禁用命令
本文关键字:命令 启用 执行 | 更新日期: 2023-09-27 18:37:04
我已经有一段时间了,我在任何地方都找不到解决方案。我目前正在为 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() 方法中处理这个问题,但Microsoft关于这个主题的文档不太有用或大开眼界。
您需要将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)
);
}
资源:
- 如何:控制 Visual Studio 加载项中的命令状态
- 有关 Visual Studio .NET 扩展性的资源
好吧,我想出了一个解决方案,尽管我不太确定它是否优雅。在命令之后(例如 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;
}
}
}
无论如何,谢谢沙利亚索斯的帮助。我很想投票给你的答案,但由于我落后于声誉点,我不能。