Пример #1
0
        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);
            }

            IMessageTrigger trigger = null;

            if (triggerPlusMessage.Length == 1)
            {
                var defaults = _controller.FindDefaultsOrFail(target);
                trigger = defaults.GetDefaultTrigger();
            }
            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)
            {
                throw new CaliburnException("Could not determine trigger type.");
            }

            trigger.Message = message;

            return(trigger);
        }
Пример #2
0
        /// <summary>
        /// Locates and perofrms type coercion for the required parameters.
        /// </summary>
        /// <param name="requiredParameters">The required parameters.</param>
        /// <param name="sourceNode">The source node.</param>
        /// <param name="handlingNode">The handling node.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        protected virtual object[] LocateAndCoerceValues(IList <RequiredParameter> requiredParameters, IInteractionNode sourceNode, IInteractionNode handlingNode, object context)
        {
            var values = new object[requiredParameters.Count];

            for (int i = 0; i < requiredParameters.Count; i++)
            {
                var    parameter = requiredParameters[i];
                object value;

                if (!DetermineSpecialValue(parameter.Name.ToLower(), sourceNode, context, out value))
                {
                    var element = handlingNode.UIElement as FrameworkElement;

                    if (element != null)
                    {
                        var control  = element.FindNameExhaustive <object>(parameter.Name, true);
                        var defaults = _routedMessageController.FindDefaultsOrFail(control);
                        value = defaults.GetDefaultValue(control);
                    }
#if !SILVERLIGHT
                    else
                    {
                        var fce = handlingNode.UIElement as FrameworkContentElement;

                        if (fce == null)
                        {
                            throw new CaliburnException(
                                      "Cannot determine parameters unless handler node is a FrameworkElement or FrameworkContentElement.");
                        }

                        var control  = fce.FindNameExhaustive <object>(parameter.Name, true);
                        var defaults = _routedMessageController.FindDefaultsOrFail(control);
                        value = defaults.GetDefaultValue(control);
                    }
#else
                    else
                    {
                        throw new CaliburnException(
                                  "Cannot determine parameters unless handler node is a FrameworkElement.");
                    }
#endif
                }

                values[i] = CoerceParameter(parameter, value);
            }
Пример #3
0
        /// <summary>
        /// Executes the custom code.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="handlingNode">The handling node.</param>
        public virtual void Execute(IRoutedMessageWithOutcome message, IInteractionNode handlingNode)
        {
            if (_outcome.WasCancelled)
            {
                Completed(this, null);
                return;
            }

            var element = message.Source.UIElement as FrameworkElement;

            if (element != null)
            {
                if (string.IsNullOrEmpty(message.OutcomePath))
                {
                    var target = element.FindNameExhaustive <object>(message.DefaultOutcomeElement, false);

                    if (target != null)
                    {
                        var defaults = _routedMessageController.FindDefaultsOrFail(target);
                        defaults.SetDefaultValue(target, _outcome.Result);
                    }
                }
                else
                {
                    var parts = message.OutcomePath.Split(_separator, StringSplitOptions.RemoveEmptyEntries);

                    if (parts.Length < 2)
                    {
                        Completed(this, new CaliburnException(string.Format("{0} is not a valid return path.", 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 CaliburnException(string.Format("{0} is not a valid property path.", parts.Skip(1).Aggregate((a, c) => a + c))));
                        return;
                    }

                    setter(_outcome.Result);
                }
            }
#if !SILVERLIGHT
            else
            {
                var fce = message.Source.UIElement as FrameworkContentElement;

                if (fce != null)
                {
                    if (string.IsNullOrEmpty(message.OutcomePath))
                    {
                        var target = fce.FindNameExhaustive <object>(message.DefaultOutcomeElement, true);

                        if (target != null)
                        {
                            var defaults = _routedMessageController.FindDefaultsOrFail(target);
                            defaults.SetDefaultValue(target, _outcome.Result);
                        }
                    }
                    else
                    {
                        var parts = message.OutcomePath.Split(_separator, StringSplitOptions.RemoveEmptyEntries);

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

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

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

                        setter(_outcome.Result);
                    }
                }
                else if (!string.IsNullOrEmpty(message.OutcomePath))
                {
                    Completed(this, new CaliburnException("Cannot determine parameters unless handler node is a FrameworkElement or FrameworkContentElement."));
                    return;
                }
            }