/// <summary>
        /// Ensures specified command is either: present and configured correctly on the element, or removed from element, based on the evaluation of the given function.
        /// </summary>
        public static CommandSettings EnsureCommandAutomation <TCommand>(this IPatternElementSchema container, string instanceName, Func <bool> exists) where TCommand : ICommand
        {
            var settings = container.GetAutomationSettings <CommandSettings>(instanceName);

            if (exists() == true)
            {
                // Create a new instance of not already exists.
                if (settings == null)
                {
                    settings = PatternElementSchemaExtensions.CreateSystemAutomationSettings <CommandSettings>(container, instanceName);
                }

                // Update properties
                if (settings != null)
                {
                    settings.TypeId = typeof(TCommand).FullName;
                }
            }
            else
            {
                // Delete existing command
                if (settings != null)
                {
                    settings.Extends.Delete();
                    return(null);
                }
            }

            return(settings);
        }
        /// <summary>
        /// Ensures specified event is either: present and configured correctly on the element, or removed from element, based on the evaluation of the given function.
        /// </summary>
        public static EventSettings EnsureEventLaunchPoint(this IPatternElementSchema container, string eventTypeName, string instanceName, CommandSettings command, Func <bool> exists)
        {
            var settings = container.GetAutomationSettings <EventSettings>(instanceName);

            if (exists() == true)
            {
                // Create a new instance of not already exists.
                if (settings == null)
                {
                    settings = PatternElementSchemaExtensions.CreateSystemAutomationSettings <EventSettings>(container, instanceName);
                }

                // Update properties
                if (settings != null)
                {
                    settings.EventId = eventTypeName;
                    if (command != null)
                    {
                        settings.CommandId = command.Id;
                    }
                }
            }
            else
            {
                // Delete existing instance
                if (settings != null)
                {
                    settings.Extends.Delete();
                    return(null);
                }
            }

            return(settings);
        }
        /// <summary>
        /// Ensures specified menu is either: present and configured correctly on the element, or removed from element, based on the evaluation of the given function.
        /// </summary>
        public static MenuSettings EnsureMenuLaunchPoint(this IPatternElementSchema container, string instanceName, CommandSettings command, string menuText, string iconPath, Func <bool> exists)
        {
            var settings = container.GetAutomationSettings <MenuSettings>(instanceName);

            if (exists() == true)
            {
                // Create a new instance of not already exists.
                if (settings == null)
                {
                    settings = PatternElementSchemaExtensions.CreateSystemAutomationSettings <MenuSettings>(container, instanceName);
                }

                // Update properties
                if (settings != null)
                {
                    settings.Text      = menuText;
                    settings.SortOrder = 10000; // This to make it large enough to sink to bottom of other menus
                    settings.Icon      = FormatIconPath(iconPath);
                    if (command != null)
                    {
                        settings.CommandId = command.Id;
                    }
                }
            }
            else
            {
                // Delete existing instance
                if (settings != null)
                {
                    settings.Extends.Delete();
                    return(null);
                }
            }

            return(settings);
        }