Jose Manuel Ojeda
Jose Manuel Ojeda

Reputation: 328

Visual Studio extension disable command

I have created an extension on visual studio 2019, the extension contains 1 command, so far so good the extension is working perfectly. I want to improve my extension by disabling or enable (or show and hide) the menu item associated with the command depending on the type of selected items Since I’m using the new version of the studio my package class inherits from AsyncPackage, at the moment I have tried to wire the OnChange event on the InitializeAsync method but the event does not trigger also I tried to use the same code on the InitializeAsync method of my command and the result is the same it never triggers

    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
    }

I have also tried to an OleMenuCommand and use the event BeforeQueryStatus but it also never trigger, here is the code I'm using for the 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";
        }
    }

you can find a full copy of the project in my github https://github.com/egarim/DisableCommandMenu

Upvotes: 0

Views: 632

Answers (2)

Jose Manuel Ojeda
Jose Manuel Ojeda

Reputation: 328

The easiest way to hide or show menu items is by using visibility constraints as shown in their sample extensions repository here VSSDK-Extensibility-Samples

Upvotes: 0

Sergey Vlasov
Sergey Vlasov

Reputation: 27880

You should add <CommandFlag>DynamicVisibility</CommandFlag> to your button command in .vsct.

Upvotes: 1

Related Questions