简体   繁体   中英

VS2013 SDK: Initialize extension, how and when to?

The problem I have is that my extension, which is a default Visual Studio Package project of a command menu, its Initialize method is call when I do click on the command menu name from the Tools menu, but that is not what I want, I just pretend to create an extension that adds a menu on the contextmenu of the code editor window (not on the Tools menu), then, how I can initialize my extension properly? when a project loads or when I do right-click in the code editor window (I don't know exactlly when I should initialize the extension neither how to initialize it).

This is the relevant code:

<PackageRegistration(UseManagedResourcesOnly:=True)>
<InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400)>
<ProvideMenuResource("Menus.ctmenu", 1)>
<Guid(GuidList.guidVSPackage2PkgString)>
Public NotInheritable Class ElektroDocPackage : Inherits Package

#Region " Constructors "

    ''' <summary>
    ''' Initializes a new instance of the <see cref="ElektroDocPackage"/> class.
    ''' </summary>
    Public Sub New()

        ' Inside this method you can place any initialization code that does not require 
        ' any Visual Studio service because at this point the package object is created but 
        ' not sited yet inside Visual Studio environment. 

    End Sub

#End Region

#Region " Overriden Methods "

    ''' <summary>
    ''' Called when the VSPackage is loaded by Visual Studio.
    ''' This is the place where you can put all the initialization code that rely on services provided by VisualStudio.
    ''' </summary>
    Protected Overrides Sub Initialize()

        MyBase.Initialize()

        Me.CreateContextMenu()

    End Sub

#End Region

#Region " Private Methods "

    ''' <summary>
    ''' Creates the context menu.
    ''' </summary>
    Private Sub CreateContextMenu()

        Dim applicationObject As DTE2 =
            DirectCast(MyBase.GetService(GetType(DTE)), DTE2)

        ' Get a reference to the context menu of code window.
        Dim codeWindowCommandBar As CommandBar =
            DirectCast(applicationObject.CommandBars, CommandBars)("Code Window")

        ' Add a popup command bar.
        Dim mainPopup As CommandBarPopup =
            DirectCast(codeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup,
                                                         Type.Missing, Type.Missing,
                                                         Type.Missing, Type.Missing), CommandBarPopup)

        mainPopup.Caption = "ElektroDoc"

        ' Add controls to the popup command bar.
        btCodeExample = DirectCast(mainPopup.Controls.Add(MsoControlType.msoControlButton,
                                                          Missing.Value, Missing.Value,
                                                          1, True), CommandBarButton)

        btCodeExample.Caption = "Code example"
        btCodeExample.Style = MsoButtonStyle.msoButtonIcon

    End Sub

#End Region

End Class

#End Region

The asnwer to my question can be found here:

HOWTO: Autoload a Visual Studio package. By Carlos J. Quintero, MZ-Tools articles

(...)
[ProvideAutoLoad(VSConstants.UICONTEXT.NoSolution_string)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasMultipleProjects_string)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasSingleProject_string)]
[Guid(GuidList.guidMyVSPackagePkgString)]
public sealed class MyVSPackagePackage : Package
{
   (...)
}

Thanks to Carlos J. Quintero .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM