繁体   English   中英

如何在Visual Studio 2017 VSIX C#项目中启用/禁用命令

[英]How can I enable/disable command in Visual Studio 2017 VSIX C# project

我正在使用C#VSIX项目开发Visual Studio 2017的扩展。 我需要基于.ini文件中的设置来创建可变数量的命令。 我想创建最大数量的命令(因为在VSIX项目中,每个命令都需要一个新的.cs文件),并且仅启用在.ini文件中编写的命令。 不幸的是,我不知道如何禁用命令。 当布尔值变为true时,我需要启用命令。

我已经看到我需要使用OleMenuCommand类,但是我没有Initialize()和StatusQuery()方法。 如何动态启用命令?

为了能够在Visual Studio /禁用命令,您可以订阅BeforeQueryStatus你的事件OleMenuCommand

myOleMenuCommand.BeforeQueryStatus += QueryCommandHandler;

private void QueryCommandHandler(object sender)
{
        var menuCommand = sender as Microsoft.VisualStudio.Shell.OleMenuCommand;
        if (menuCommand != null)
            menuCommand.Visible = menuCommand.Enabled = MyCommandStatus();
}

MyCommandStatus()方法的可能实现可以是:

public bool MyCommandStatus()
{
    // do this if you want to disable your commands when the solution is not loaded
    if (false == mDte.Solution.IsOpen)
      return false;

    // do this if you want to disable your commands when the Visual Studio build is running
    else if (true == VsBuildRunning)
      return false;

    // Write any condition here

    return true;

}

创建要与OleMenuCommandService一起添加的OleMenuCommand时,可以预订BeforeQueryStatus事件,并在其中动态启用/禁用该命令:

    private void OnQueryStatus(object sender)
    {
            Microsoft.VisualStudio.Shell.OleMenuCommand menuCommand =
                sender as Microsoft.VisualStudio.Shell.OleMenuCommand;
            if (menuCommand != null)
                menuCommand.Visible = menuCommand.Enabled = MyCommandStatus();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM