Compartir a través de


Loading VSPackages

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

VSPackages are loaded into Visual Studio only when their functionality is required. For example, a VSPackage is loaded when Visual Studio uses a project factory or a service that the VSPackage implements. This feature is called delayed loading, which is used whenever possible to improve performance.

Note

Visual Studio can determine certain VSPackage information, such as the commands that a VSPackage offers, without loading the VSPackage.

VSPackages can be set to autoload in a particular user interface (UI) context, for example, when a solution is open. The ProvideAutoLoadAttribute attribute sets this context.

Autoloading a VSPackage in a specific context

  • Add the ProvideAutoLoad attribute to the VSPackage attributes:

    [DefaultRegistryRoot(@"Software\Microsoft\VisualStudio\14.0")]  
    [PackageRegistration(UseManagedResourcesOnly = true)]  
    [ProvideAutoLoad(UIContextGuids80.SolutionExists)]  
    [Guid("00000000-0000-0000-0000-000000000000")] // your specific package GUID  
    public class MyAutoloadedPackage : Package  
    {. . .}  
    

    See the enumerated fields of UIContextGuids80 for a list of the UI contexts and their GUID values.

  • Set a breakpoint in the Initialize method.

  • Build the VSPackage and start debugging.

  • Load a solution or create one.

    The VSPackage loads and stops at the breakpoint.

Forcing a VSPackage to load

Under some circumstances a VSPackage may have to force another VSPackage to be loaded. For example, a lightweight VSPackage might load a larger VSPackage in a context that is not available as a CMDUIContext.

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

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

    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.

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

Using a custom attribute to register a VSPackage

In certain cases you may need to create a new registration attribute for your extension. You can use registration attributes to add new registry keys or to add new values to existing keys. The new attribute must derive from RegistrationAttribute, and it must override the Register and Unregister methods.

Creating a Registry Key

In the following code, the custom attribute creates a Custom subkey under the key for the VSPackage that is being registered.

public override void Register(RegistrationAttribute.RegistrationContext context)  
{  
    Key packageKey = null;  
    try  
    {   
        packageKey = context.CreateKey(@"Packages\{" + context.ComponentType.GUID + @"}\Custom");  
        packageKey.SetValue("NewCustom", 1);  
    }  
    finally  
    {  
        if (packageKey != null)  
            packageKey.Close();  
    }  
}  
  
public override void Unregister(RegistrationContext context)  
{  
    context.RemoveKey(@"Packages\" + context.ComponentType.GUID + @"}\Custom");  
}  
  

Creating a New Value Under an Existing Registry Key

You can add custom values to an existing key. The following code shows how to add a new value to a VSPackage registration key.

public override void Register(RegistrationAttribute.RegistrationContext context)  
{  
    Key packageKey = null;  
    try  
    {   
        packageKey = context.CreateKey(@"Packages\{" + context.ComponentType.GUID + "}");  
        packageKey.SetValue("NewCustom", 1);  
    }  
    finally  
    {  
        if (packageKey != null)  
            packageKey.Close();  
                }  
}  
  
public override void Unregister(RegistrationContext context)  
{  
    context.RemoveValue(@"Packages\" + context.ComponentType.GUID, "NewCustom");  
}  

See Also

VSPackages