我已经在Visual Studio 2019上创建了一个扩展,该扩展包含1个命令,到目前为止到目前为止,该扩展运行良好。 我想通过根据所选项目的类型禁用或启用(或显示和隐藏)与命令关联的菜单项来改进扩展名 由于我使用的是Studio的新版本,因此我的包类继承自AsyncPackage,此刻,我尝试在InitializeAsync方法上连接OnChange事件,但该事件未触发,因此我尝试在InitializeAsync上使用相同的代码我的命令的方法,结果是永远不会触发的结果
protected override async Task InitializeAsync(CancellationToken cancellationToken,
IProgress<ServiceProgressData> progress)
{
// When initialized asynchronously, the current thread may be a background thread at this point.
// Do any initialization that requires the UI thread after switching to the UI thread.
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
await ExampleCommand.InitializeAsync(this);
_dte2 = ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE2;
_dteEvents = _dte2.Events as Events2;
_selectionEvents = _dteEvents.SelectionEvents;
_selectionEvents.OnChange += SelectionEventsOnOnChange;
}
DTE2 _dte2;
Events2 _dteEvents;
SelectionEvents _selectionEvents;
void SelectionEventsOnOnChange()
{
//This method never triggers
}
我也尝试过使用OleMenuCommand并使用事件BeforeQueryStatus,但它也从未触发,这是我用于OleMenuCommand的代码
ExampleCommand(AsyncPackage package, OleMenuCommandService commandService)
{
this.package = package ?? throw new ArgumentNullException(nameof(package));
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
global::System.ComponentModel.Design.CommandID menuCommandID = new CommandID(CommandSet, CommandId);
menuItem = new OleMenuCommand(Execute, menuCommandID);
menuItem.BeforeQueryStatus += MenuItem_BeforeQueryStatus;
commandService.AddCommand(menuItem);
}
void MenuItem_BeforeQueryStatus(object sender, EventArgs e)
{
OleMenuCommand myCommand = sender as OleMenuCommand;
if (null != myCommand)
{
myCommand.Text = "New Text";
}
}
您可以在我的github中找到该项目的完整副本 https://github.com/egarim/DisableCommandMenu
答案 0 :(得分:1)
您应该在.vsct中的按钮命令中添加
答案 1 :(得分:0)
隐藏或显示菜单项的最简单方法是使用可见性约束,如此处VSSDK-Extensibility-Samples
的示例扩展存储库中所示。