Condividi tramite


How to: Force a VSPackage to Load

VSPackages are ordinarily loaded only when their accompanying functionality is required to complete a process. Under some circumstances, however, a VSPackage may have to force another VSPackage to be loaded. For example, a lightweight VSPackage might load a larger VSPackage in a programming context that is not available as a CMDUIContext.

You can use the LoadPackage method to force a VSPackage to load.

To force a VSPackage to load

  • Insert this code into the Initialize method of the VSPackage that forces another VSPackage to load:

    Dim shell As IVsShell = TryCast(GetService(GetType(SVsShell)), IVsShell)
    If shell Is Nothing Then
        Return
    End If
    
    Dim package As IVsPackage = Nothing
    Dim PackageToBeLoadedGuid As Guid = GetType(PackageToBeLoaded).GUID
    shell.LoadPackage(PackageToBeLoadedGuid, package)
    
    IVsShell shell = GetService(typeof(SVsShell)) as IVsShell;
    if (shell == null) return;
    
    IVsPackage package = null;
    Guid PackageToBeLoadedGuid = 
        new Guid(Microsoft.PackageToBeLoaded.GuidList.guidPackageToBeLoadedPkgString);
    shell.LoadPackage(ref PackageToBeLoadedGuid, out package);
    

    When the VSPackage is initialized, it will force PackageToBeLoaded to load.

Robust Programming

Force loading should not be used for VSPackage communication. Use Services instead.

See Also

Concepts

Loading VSPackages

Other Resources

VSPackages