Exemplo n.º 1
0
        /// <summary>
        /// Parses the specified message text.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="text">The message text.</param>
        /// <returns>The triggers parsed from the text.</returns>
        public static IEnumerable <TriggerBase> Parse(DependencyObject target, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(new TriggerBase[0]);
            }

            var triggers     = new List <TriggerBase>();
            var messageTexts = StringSplitter.Split(text, ';');

            foreach (var messageText in messageTexts)
            {
                var triggerPlusMessage = LongFormatRegularExpression.IsMatch(messageText)
                                             ? StringSplitter.Split(messageText, '=')
                                             : new[] { null, messageText };

                var messageDetail = triggerPlusMessage.Last()
                                    .Replace("[", string.Empty)
                                    .Replace("]", string.Empty)
                                    .Trim();

                var trigger = CreateTrigger(target, triggerPlusMessage.Length == 1 ? null : triggerPlusMessage[0]);
                var message = CreateMessage(target, messageDetail);

#if WINDOWS_UWP || XFORMS
                AddActionToTrigger(target, message, trigger);
#else
                trigger.Actions.Add(message);
#endif

                triggers.Add(trigger);
            }

            return(triggers);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an instance of <see cref="ActionMessage"/> by parsing out the textual dsl.
        /// </summary>
        /// <param name="target">The target of the message.</param>
        /// <param name="messageText">The textual message dsl.</param>
        /// <returns>The created message.</returns>
        public static TriggerAction CreateMessage(DependencyObject target, string messageText)
        {
            var openingParenthesisIndex = messageText.IndexOf('(');

            if (openingParenthesisIndex < 0)
            {
                openingParenthesisIndex = messageText.Length;
            }

            var closingParenthesisIndex = messageText.LastIndexOf(')');

            if (closingParenthesisIndex < 0)
            {
                closingParenthesisIndex = messageText.Length;
            }

            var core           = messageText.Substring(0, openingParenthesisIndex).Trim();
            var message        = InterpretMessageText(target, core);
            var withParameters = message as IHaveParameters;

            if (withParameters != null)
            {
                if (closingParenthesisIndex - openingParenthesisIndex > 1)
                {
                    var paramString = messageText.Substring(openingParenthesisIndex + 1, closingParenthesisIndex - openingParenthesisIndex - 1);
                    var parameters  = StringSplitter.SplitParameters(paramString);

                    foreach (var parameter in parameters)
                    {
                        withParameters.Parameters.Add(CreateParameter(target, parameter.Trim()));
                    }
                }
            }

            return(message);
        }