locked
Getting a package to site when the project loads. RRS feed

  • Question

  • I'm a little confused as to when a package will "site" and therefore hit my extension's Initialize() function.

    What does "site" mean anyways?  Why not just call it load?

    It seems my package only sites after I have clicked on the menu item in View -> Other Windows.  That is useless to me as I am building a post build tool, and I don't want to have to click the menu item every time I want to load build settings.  I want settings to load from a config file when the project loads.

    What I want is Initialize() to hit when the project loads.  I understand about the OnProjectLoad() event handlers, but without Initialize() hitting, I can't subscribe to them.

    How can I get Initialize() to hit, in my package, when the project loads?

    Thanks for any information.


    Taylor Deiaco Developer

    Tuesday, July 12, 2016 5:59 PM

Answers

  • Hi Taylor,

    Beneath the MPF classes, your package object implements the IVsPackage interface. The IVsPackage.SetSite method, which the IDE will invoke when your package is first loaded, passing your package an IServiceProvider interface from which you can retrieve additional service interfaces.

    A lot of plumbing is in the base VSPackage class you derive from, and abstracted by that virtual Initialize method.

    What I suspect you're asking about here is how to specify when your package is loaded. That is done by adding ProvideAutoLoad attributes to your package class. This attribute takes a context guid, which is what you use to specify when the IDE should first load your package.

    Note, by default packages are loaded on demand. As you can imagine with hundreds of packages, you don't want to load the ones you aren't using, as that would have a dramatic impact on the startup and memory requirements of the IDE. So use a context guid appropriate for your scenario. If you are just adding menu items, don't sweat it. The package will load when the IDE realizes the menu item you select is associated with your package.

    If you need to load sooner (for example you might want to control the appearance of the menu item), use a context that makes sense. Like when a certain project type has loaded, or a specific editor has been created.

    Tip: Not all context guids are undocumented. But Paul Harrington's Component Diagnostics extension, is a super useful tool for sniffing out useful context guids.

    Sincerely,


    Ed Dore

    Wednesday, July 13, 2016 5:20 AM

All replies


  • You need to add the ProvideAutoLoad attribute to your main package class and specify a selected UIContextGuid.

    Sergey Vlasov | Vlasov Studio | Visual Studio add-ins, extensions and tools

    • Proposed as answer by DotNet Wang Thursday, July 14, 2016 5:06 AM
    Wednesday, July 13, 2016 5:12 AM
  • Hi Taylor,

    Beneath the MPF classes, your package object implements the IVsPackage interface. The IVsPackage.SetSite method, which the IDE will invoke when your package is first loaded, passing your package an IServiceProvider interface from which you can retrieve additional service interfaces.

    A lot of plumbing is in the base VSPackage class you derive from, and abstracted by that virtual Initialize method.

    What I suspect you're asking about here is how to specify when your package is loaded. That is done by adding ProvideAutoLoad attributes to your package class. This attribute takes a context guid, which is what you use to specify when the IDE should first load your package.

    Note, by default packages are loaded on demand. As you can imagine with hundreds of packages, you don't want to load the ones you aren't using, as that would have a dramatic impact on the startup and memory requirements of the IDE. So use a context guid appropriate for your scenario. If you are just adding menu items, don't sweat it. The package will load when the IDE realizes the menu item you select is associated with your package.

    If you need to load sooner (for example you might want to control the appearance of the menu item), use a context that makes sense. Like when a certain project type has loaded, or a specific editor has been created.

    Tip: Not all context guids are undocumented. But Paul Harrington's Component Diagnostics extension, is a super useful tool for sniffing out useful context guids.

    Sincerely,


    Ed Dore

    Wednesday, July 13, 2016 5:20 AM
  • Thanks to both of your for your speedy answers! MS++

    Taylor Deiaco Developer

    Wednesday, July 13, 2016 3:47 PM