VS2013 SDK:初始化扩展,如何以及何时?
VS2013 SDK: Initialize extension, how and when to?
我遇到的问题是我的扩展,它是命令菜单的默认 Visual Studio Package 项目,它的 Initialize
方法被调用时我确实从 工具 菜单中单击命令菜单名称,但这不是我想要的,我只是假装创建一个扩展,在代码编辑器的上下文菜单上添加一个菜单 window (不在 工具 菜单上),那么,我该如何正确初始化我的扩展?当项目加载时或当我在代码编辑器中单击鼠标右键时 window(我不知道应该何时初始化扩展,也不知道如何初始化它)。
这是相关代码:
<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
我的问题的答案可以在这里找到:
HOWTO:自动加载 Visual Studio 包。
作者:Carlos J. Quintero,MZ-Tools 文章
(...)
[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
{
(...)
}
感谢 Carlos J. Quintero。
我遇到的问题是我的扩展,它是命令菜单的默认 Visual Studio Package 项目,它的 Initialize
方法被调用时我确实从 工具 菜单中单击命令菜单名称,但这不是我想要的,我只是假装创建一个扩展,在代码编辑器的上下文菜单上添加一个菜单 window (不在 工具 菜单上),那么,我该如何正确初始化我的扩展?当项目加载时或当我在代码编辑器中单击鼠标右键时 window(我不知道应该何时初始化扩展,也不知道如何初始化它)。
这是相关代码:
<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
我的问题的答案可以在这里找到:
HOWTO:自动加载 Visual Studio 包。 作者:Carlos J. Quintero,MZ-Tools 文章
(...)
[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
{
(...)
}
感谢 Carlos J. Quintero。