/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
        /// <param term='application'>Root object of the host application.</param>
        /// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
        /// <param term='addInInst'>Object representing this Add-in.</param>
        /// <seealso class='IDTExtensibility2' />
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            try
            {
                _applicationObject = (DTE2)application;
                _addInInstance = (EnvDTE.AddIn)addInInst;

                if (!menuRegistered)
                {
                    Commands2 commands = (Commands2)_applicationObject.Commands;

                    foreach (Command cmd in commands)
                    {
                        if (cmd.Name == "EntitySpaces.AddIn.ES2012.Connect.EntitySpaces2012")
                        {
                            menuRegistered = true;
                            break;
                        }
                    }

                    if (!menuRegistered)
                    {
                        try
                        {
                            string toolsMenuName = "";

                            try
                            {
                                //If you would like to move the command to a different menu, change the word "Tools" to the 
                                //  English version of the menu. This code will take the culture, append on the name of the menu
                                //  then add the command to that menu. You can find a list of all the top-level menus in the file
                                //  CommandBar.resx.
                                string resourceName;
                                ResourceManager resourceManager = new ResourceManager("EntitySpaces.AddIn.ES2012.CommandBar", Assembly.GetExecutingAssembly());
                                CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID);

                                if (cultureInfo.TwoLetterISOLanguageName == "zh")
                                {
                                    System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
                                    resourceName = String.Concat(parentCultureInfo.Name, "Tools");
                                }
                                else
                                {
                                    resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
                                }
                                toolsMenuName = resourceManager.GetString(resourceName);
                            }
                            catch
                            {
                                //We tried to find a localized version of the word Tools, but one was not found.
                                //  Default to the en-US word, which may work for the current culture.
                                toolsMenuName = "Tools";
                            }

                            //Place the command on the tools menu.
                            //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
                            Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

                            //Find the Tools command bar on the MenuBar command bar:
                            CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
                            CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

                            object[] contextGUIDS = new object[] { };

                            Command command = commands.AddNamedCommand2(_addInInstance, "EntitySpaces2012", "EntitySpaces 2012", "Open the EntitySpaces 2012 Window", true, 1018,
                                ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

                            //Add a control for the command to the tools menu:
                            if ((command != null) && (toolsPopup != null))
                            {
                                command.AddControl(toolsPopup.CommandBar, 1);
                                menuRegistered = true;
                            }
                        }
                        catch (System.ArgumentException eee)
                        {
                           
                        }
                    }
                }

                if (esWindow == null)
                {
                    object programmableObject = null;

                    string guidString = "{08C42F7C-B8FB-4c8b-B364-37F7E04E78E7}";
                    Windows2 windows2 = (Windows2)_applicationObject.Windows;
                    Assembly asm = Assembly.GetExecutingAssembly();
                    toolWindow = windows2.CreateToolWindow2(_addInInstance, asm.Location, "EntitySpaces.AddIn.ES2012.MainWindow", "EntitySpaces 2012",
                        guidString, ref programmableObject);
                    toolWindow.Visible = true;
                    esWindow = (MainWindow)toolWindow.Object;
                    esWindow.ApplicationObject = _applicationObject;
                }
            }
            catch { }
        }
 static internal void Initialize(MainWindow mainWindow)
 {
     TemplateDisplaySurface.MainWindow = mainWindow;
 }