Example #1
0
        /// <summary>
        /// Executes the result within the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public virtual void Execute(ResultExecutionContext context)
        {
            if (outcome.WasCancelled)
            {
                Completed(this, new ResultCompletionEventArgs {
                    WasCancelled = outcome.WasCancelled
                });
                return;
            }

            var element = context.Message.Source.UIElement;

            if (string.IsNullOrEmpty(context.Message.OutcomePath))
            {
                var target = element.FindNameExhaustive <DependencyObject>(context.Message.DefaultOutcomeElement, false);

                if (target != null)
                {
                    var defaults = conventionManager.FindElementConventionOrFail(target);
                    defaults.SetValue(target, outcome.Result);
                }
            }
            else
            {
                var parts = context.Message.OutcomePath.Split(Separator, StringSplitOptions.RemoveEmptyEntries);

                if (parts.Length < 2)
                {
                    Completed(
                        this,
                        new ResultCompletionEventArgs
                    {
                        Error = new CaliburnException(
                            string.Format("{0} is not a valid return path.", context.Message.OutcomePath)
                            )
                    });
                    return;
                }

                var target = element.FindNameExhaustive <object>(parts[0], true);
                var setter = CreateSetter(target, parts.Skip(1).ToArray());

                if (setter == null)
                {
                    Completed(
                        this,
                        new ResultCompletionEventArgs
                    {
                        Error = new CaliburnException(
                            string.Format("{0} is not a valid property path.", parts.Skip(1).Aggregate((a, c) => a + c))
                            )
                    });
                    return;
                }

                setter(outcome.Result);
            }

            Completed(this, new ResultCompletionEventArgs());
        }
Example #2
0
        /// <summary>
        /// Parses the specified message text.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="messageText">The message text.</param>
        /// <returns>The triggers parsed from the text.</returns>
        protected virtual IMessageTrigger ParseMessage(string messageText, DependencyObject target)
        {
            var    triggerPlusMessage = messageText.Split('=');
            string messageDetail      = triggerPlusMessage.Last()
                                        .Replace("[", string.Empty)
                                        .Replace("]", string.Empty)
                                        .Trim();

            IRoutedMessage message = null;

            foreach (var keyValuePair in messageParsers)
            {
                if (messageDetail.StartsWith(keyValuePair.Key, StringComparison.CurrentCultureIgnoreCase))
                {
                    message = keyValuePair.Value
                              .Parse(target, messageDetail.Remove(0, keyValuePair.Key.Length).Trim());
                    break;
                }
            }

            if (message == null)
            {
                message = messageParsers[defaultMessageParserKey]
                          .Parse(target, messageDetail);
                Log.Info("Using default parser {0} for {1} on {2}.", defaultMessageParserKey, messageText, target);
            }

            IMessageTrigger trigger = null;

            if (triggerPlusMessage.Length == 1)
            {
                var defaults = conventionManager.FindElementConventionOrFail(target);
                trigger = defaults.CreateTrigger();
                Log.Info("Using default trigger {0} for {1} on {2}.", trigger, messageText, target);
            }
            else
            {
                var triggerDetail = triggerPlusMessage[0]
                                    .Replace("[", string.Empty)
                                    .Replace("]", string.Empty)
                                    .Trim();

                foreach (var keyValuePair in triggerParsers)
                {
                    if (triggerDetail.StartsWith(keyValuePair.Key, StringComparison.CurrentCultureIgnoreCase))
                    {
                        trigger = keyValuePair.Value
                                  .Parse(target, triggerDetail.Remove(0, keyValuePair.Key.Length).Trim());
                        break;
                    }
                }
            }

            if (trigger == null)
            {
                var exception = new CaliburnException("Could not determine trigger type.");
                Log.Error(exception);
                throw exception;
            }

            trigger.Message = message;

            return(trigger);
        }