从插件工具栏隐藏按钮 [英] hiding a button from a plugin toolbar
问题描述
我正在用C#开发Visual Studio的插件.该插件有一个设置页面和一个工具栏中的几个按钮,可以调用命令.
I am developping a plugin for Visual Studio in C#. The plugin has a settings page and a few buttons in a toolbar to call commands.
问题是,在某些情况下,我想隐藏特定的按钮.我能做的最好的就是禁用该按钮.
The problem is that in some circonstances, I want to hide a specific button. The best I was able to do is to disable the button.
是否可以动态更改其可见性?
Is it possible to dynamically change it's visibility?
我从手机上写了这个问题,所以也许没有足够的细节...
I wrote this question from my mobile, so maybe there is not enough details...
我在.vsct文件中创建工具栏(我在同一文件中创建菜单)
I create the toolbar in the .vsct file (I created the menu in the same file)
<Button guid="guidProductCmdSet" id="startCommand" priority="0x0100" type="Button">
<Parent guid="guidProductCmdSet" id="ToolbarGroup1" />
<Icon guid="Icons" id="startIcon" />
<Strings>
<CommandName>startCommand</CommandName>
<ButtonText>Start</ButtonText>
</Strings>
</Button>
扩展名初始化后,我创建命令:
When the extension initializes, I create the commands:
var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (null != mcs)
{
_startCommandId = new CommandID(GuidList.guidProducyVSICmdSet, (int)pkgCmdIDList.startCommand);
var startItem = new MenuCommand(StartProcess, _startCommandId);
mcs.AddCommand(startItem);
}
之后,我可以禁用工具栏中的某些按钮,如下所示:
After, I am able to disable some buttons from the toolbar like this:
var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
var mc = mcs.FindCommand(commandId);
if (mc != null)
{
mc.Enabled = false;
}
我尝试了mc.Visible = false,但它什么也没做.
I tried mc.Visible = false, but it does nothing.
推荐答案
显然,这可行...
var commandBars = ((CommandBars)_dte2.CommandBars);
if (commandBars == null)
{
return;
}
var commandBar = commandBars["MyPluginProductName"];
if (commandBar == null)
{
return;
}
var startButton = commandBar.Controls["startCommand"];
if (startButton == null)
{
return;
}
startButton.Visible = false;
这篇关于从插件工具栏隐藏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!