Visual Studio 扩展禁用命令
Visual Studio extension disable command
我在 visual studio 2019 年创建了一个扩展程序,该扩展程序包含 1 个命令,到目前为止,该扩展程序运行良好。
我想通过根据所选项目的类型禁用或启用(或显示和隐藏)与命令关联的菜单项来改进我的扩展
由于我使用的是新版本的工作室,我的包 class 继承自 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
您应该将 DynamicVisibility 添加到 .vsct 中的按钮命令。
隐藏或显示菜单项的最简单方法是使用可见性约束,如此处的示例扩展存储库所示 VSSDK-Extensibility-Samples
我在 visual studio 2019 年创建了一个扩展程序,该扩展程序包含 1 个命令,到目前为止,该扩展程序运行良好。 我想通过根据所选项目的类型禁用或启用(或显示和隐藏)与命令关联的菜单项来改进我的扩展 由于我使用的是新版本的工作室,我的包 class 继承自 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
您应该将
隐藏或显示菜单项的最简单方法是使用可见性约束,如此处的示例扩展存储库所示 VSSDK-Extensibility-Samples