public void Associate(MetaFeature metaFeature, ITypedServiceProvider serviceProvider) { if (m_metaCommand != null || m_serviceProvider != null) { throw new InvalidOperationException("Associate should only be called once on a IFeature instance."); } if (metaFeature == null) { throw new ArgumentNullException("metaFeature"); } if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } m_metaCommand = metaFeature as MetaCommand; m_serviceProvider = serviceProvider; IMenuCommandService menuService = m_serviceProvider.GetService <IMenuCommandService>(); Debug.Assert(menuService != null); if (menuService != null) { menuService.AddCommand(this); } }
/// <summary> /// Uninstalls specified command from the product. /// </summary> /// <param name="metaFeature">MetaCommand to uninstall.</param> public void Uninstall(MetaFeature metaFeature) { MetaCommand metaCommand = metaFeature as MetaCommand; Debug.Assert(metaCommand != null); if (metaCommand != null && metaCommand.IsCustomCommand) { string canonicalName = m_commandService.GetCanonicalName(metaCommand.Name); if (metaCommand.CustomCommandId == null) { metaCommand.CustomCommandId = m_commandService.FindCommandId(canonicalName); } if (metaCommand.CustomCommandId != null) { m_commandService.DeleteCommand(canonicalName); metaCommand.CustomCommandId = null; } } }
/// <summary> /// Checks whether given command is already installed. /// </summary> /// <param name="metaFeature">MetaCommand.</param> /// <returns>True if installed and false otherwise.</returns> public bool IsInstalled(MetaFeature metaFeature) { MetaCommand metaCommand = metaFeature as MetaCommand; Debug.Assert(metaCommand != null); if (metaCommand != null) { if (metaCommand.IsCustomCommand) { if (metaCommand.CustomCommandId == null) { metaCommand.CustomCommandId = m_commandService.FindCommandId(m_commandService.GetCanonicalName(metaCommand.Name)); } return(metaCommand.CustomCommandId != null); } else { return(true); } } return(false); }
protected virtual void Dispose(bool disposing) { Debug.Assert(disposing, "Finalizing CustomCommand without disposing."); if (disposing) { if (m_serviceProvider != null) { IMenuCommandService menuService = m_serviceProvider.GetService <IMenuCommandService>(); if (menuService != null) { menuService.RemoveCommand(this); } m_serviceProvider = null; } } m_metaCommand = null; m_selection = null; Debug.WriteLine("Unloaded and disposed command " + m_metaCommand); }
/// <summary> /// Installs the command into the product. /// </summary> /// <param name="metaFeature">MetaCommand to install.</param> public void Install(MetaFeature metaFeature) { MetaCommand metaCommand = metaFeature as MetaCommand; Debug.Assert(metaCommand != null && !IsInstalled(metaCommand)); if (metaCommand != null && metaCommand.IsCustomCommand) { string canonicalName = m_commandService.GetCanonicalName(metaCommand.Name); if (metaCommand.CustomCommandId == null) { metaCommand.CustomCommandId = m_commandService.FindCommandId(canonicalName); if (metaCommand.CustomCommandId == null) { metaCommand.CustomCommandId = m_commandService.CreateCommand(canonicalName, metaCommand.Name, metaCommand.Description); } } foreach (CommandPlacementAttribute attr in ReflectionHelper.GetAttributes <CommandPlacementAttribute>(metaCommand.FeatureType)) { m_commandService.PlaceCommand(canonicalName, attr.CommandBarPath); } } }