Condividi tramite


How to: Update the User Interface

After you implement a command-handling scheme in your VSPackage, you can add code to update the user interface (UI) with the state of your new commands.

In a typical Win32 application, the command set can be continuously polled and the state of individual commands can be adjusted as the user views them. However, because the Visual Studio shell can host an unlimited number of VSPackages, extensive polling might decrease responsiveness, especially polling across interop assemblies between managed code and COM. 

To update the UI

  • Perform one of the following steps:

    • Call the UpdateCommandUI method.

      An IVsUIShell interface can be obtained from the SVsUIShell service, as follows.

      Private Sub UpdateUI(ByVal sp As Microsoft.VisualStudio.Shell.ServiceProvider)
          Dim vsShell As IVsUIShell = DirectCast(sp.GetService(GetType(IVsUIShell)), IVsUIShell)
          If vsShell IsNot Nothing Then 
              Dim hr As Integer = vsShell.UpdateCommandUI(0)
              Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr)
          End If 
      End Sub
      
      void UpdateUI(Microsoft.VisualStudio.Shell.ServiceProvider sp)
      {
          IVsUIShell vsShell = (IVsUIShell)sp.GetService(typeof(IVsUIShell));
          if (vsShell != null)
          {
              int hr = vsShell.UpdateCommandUI(0);
              Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
          }
      }
      
      void UpdateUI(IServiceProvider *pSP)
      {
          CComPtr< IVsUIShell> srpShell;
          int hresult = pSP->QueryService(SID_SVsUIShell, 
                                          IID_IVsUIShell, 
                                          (void **)&srpShell);
          if (SUCCEEDED(hresult) && NULL != srpShell)
          {
              sprShell->UpdateCommandUI(FALSE);
          }
      }
      

      If the parameter of the UpdateCommandUI is non-zero (TRUE), then the update is performed synchronously and immediately. We recommend that you pass zero (FALSE) for this parameter to help maintain good performance. If you want to avoid caching, apply the DontCache flag when you create the command in the .vsct file. Nevertheless, use the flag cautiously or performance might decrease. For more information about command flags, see the Command Flag Element documentation.

    • In VSPackages that host an ActiveX control by using the in-place activation model in a window, it might be more convenient to use the UpdateUI method. The UpdateCommandUI method in the IVsUIShell interface and the UpdateUI method in the IOleInPlaceComponentUIManager interface are functionally equivalent. Both cause the environment to re-query the state of all commands. Typically, an update is not performed immediately. Instead, an update is delayed until idle time. The shell caches the command state to help maintain good performance. If you want to avoid caching, apply the DontCache flag when you create the command in the .vsct file. Nevertheless, use the flag cautiously because performance might decrease.

      Notice that you can obtain the IOleInPlaceComponentUIManager interface by calling the QueryInterface method on an IOleComponentUIManager object or by obtaining the interface from the SOleComponentUIManager service.

See Also

Concepts

How VSPackages Add User Interface Elements to the IDE

Command Implementation