如何在Visual Studio 2017 VSIX C#项目中启用/禁用命令 [英] How can I enable/disable command in Visual Studio 2017 VSIX C# project

查看:62
本文介绍了如何在Visual Studio 2017 VSIX C#项目中启用/禁用命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing an extension for Visual Studio 2017 using a C# VSIX project. I need to create a variable number of commands based on settings in a .ini file. I thought to create a maximum number of commands (because in a VSIX project every command needs a new .cs file), and enable only the commands written in the .ini file. Unfortunately I don't know how to disable a command. I need to enable the commands when a boolean becomes true.

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

I've seen that I need to use the OleMenuCommand class, but I do not have Initialize() and StatusQuery() methods. How can I dynamically enable my commands?

推荐答案

要在Visual Studio中启用/禁用命令,您可以订阅 OleMenuCommand BeforeQueryStatus 事件:

To enable/disable commands in Visual Studio you can subscribe to the BeforeQueryStatus event of your 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()方法的可能实现可以是:

A possible implementation of MyCommandStatus() method can be:

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;

}

这篇关于如何在Visual Studio 2017 VSIX C#项目中启用/禁用命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆