Share via


Package.Initialize Method

Called when the VSPackage is loaded by Visual Studio.

Namespace:  Microsoft.VisualStudio.Shell
Assembly:  Microsoft.VisualStudio.Shell.9.0 (in Microsoft.VisualStudio.Shell.9.0.dll)

Syntax

'Declaration
Protected Overridable Sub Initialize
'Usage
Me.Initialize()
protected virtual void Initialize()
protected:
virtual void Initialize()
protected function Initialize()

Remarks

Override Initialize when you need to carry out additional steps (such as Editor Factory registration) when the VSPackage is loaded by Visual Studio.

Place any initialization steps that require access to the global service provider (such as Editor Factory registration) in the Initialize method. Event handlers for menu and toolbar commands should also be added here.

In contrast, steps such as adding services, adding option keys, and toolbox event handlers should be placed in the Package constructor method.

Examples

The following example overrides the Initialize method to add event handlers for a menu command. The default (click) handler is passed as a parameter to the OleMenuCommand constructor, and the BeforeQueryStatus event handler is assigned on the next line.

This example is extracted from How to: Change the Text of a Menu Command.

protected override void Initialize()
{
    var mcs = GetService(typeof(IMenuCommandService)) 
        as OleMenuCommandService;
    if ( null != mcs )
    {
        var menuCommandID = new CommandID(
            GuidList.guidMenuTextCmdSet, 
            (int)PkgCmdIDList.cmdidMyTextCommand);
        var menuItem = new OleMenuCommand(
            MenuItemCallback, menuCommandID );
        menuItem.BeforeQueryStatus += 
            new EventHandler(OnBeforeQueryStatus);
        mcs.AddCommand( menuItem );
    }
}

.NET Framework Security

See Also

Reference

Package Class

Package Members

Microsoft.VisualStudio.Shell Namespace

RegisterEditorFactory