Udostępnij za pośrednictwem


Updating the User Interface

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

After you implement a command, you can add code to update the user interface 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

  1. Perform one of the following steps:

    • Call the UpdateCommandUI method.

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

      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);  
          }  
      }  
      
      

      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

How VSPackages Add User Interface Elements
Implementation