///<summary>
        ///  Uses the action pipeline to invoke the method.
        ///</summary>
        ///<param name="target"> The object instance to invoke the method on. </param>
        ///<param name="methodName"> The name of the method to invoke. </param>
        ///<param name="view"> The view. </param>
        ///<param name="source"> The source of the invocation. </param>
        ///<param name="eventArgs"> The event args. </param>
        ///<param name="parameters"> The method parameters. </param>
        public static void Invoke(object target, string methodName, DependencyObject view = null, System.Windows.FrameworkContentElement source = null, object eventArgs = null, object[] parameters = null)
        {
            var context = new ActionExecutionContext {
                Target = target,
#if WinRT
                Method = target.GetType().GetRuntimeMethods().Single(m => m.Name == methodName),
#else
                Method = target.GetType().GetMethod(methodName),
#endif
                Message = new ActionMessage {
                    MethodName = methodName
                },
                View      = view,
                Source    = source,
                EventArgs = eventArgs
            };

            if (parameters != null)
            {
                parameters.Apply(x => context.Message.Parameters.Add(x as Parameter ?? new Parameter {
                    Value = x
                }));
            }

            ActionMessage.InvokeAction(context);
        }
Beispiel #2
0
        void UpdateContext()
        {
            if (context != null)
            {
                context.Dispose();
            }

            context = new ActionExecutionContext {
                Message = this,
                Source  = AssociatedObject
            };

            PrepareContext(context);
            UpdateAvailabilityCore();
        }
Beispiel #3
0
        /// <summary>
        /// Determines the parameters that a method should be invoked with.
        /// </summary>
        /// <param name="context">The action execution context.</param>
        /// <param name="requiredParameters">The parameters required to complete the invocation.</param>
        /// <returns>The actual parameter values.</returns>
        public static object[] DetermineParameters(ActionExecutionContext context, ParameterInfo[] requiredParameters)
        {
            var providedValues = context.Message.Parameters.OfType <Parameter>().Select(x => x.Value).ToArray();
            var finalValues    = new object[requiredParameters.Length];

            for (int i = 0; i < requiredParameters.Length; i++)
            {
                var parameterType     = requiredParameters[i].ParameterType;
                var parameterValue    = providedValues[i];
                var parameterAsString = parameterValue as string;

                if (parameterAsString != null)
                {
                    finalValues[i] = CoerceValue(parameterType,
                                                 EvaluateParameter(parameterAsString, parameterType, context), context);
                }
                else
                {
                    finalValues[i] = CoerceValue(parameterType, parameterValue, context);
                }
            }

            return(finalValues);
        }
Beispiel #4
0
        /// <summary>
        /// Try to find a candidate for guard function, having:
        ///		- a name in the form "CanXXX"
        ///		- no generic parameters
        ///		- a bool return type
        ///		- no parameters or a set of parameters corresponding to the action method
        /// </summary>
        /// <param name="context">The execution context</param>
        /// <returns>A MethodInfo, if found; null otherwise</returns>
        static MethodInfo TryFindGuardMethod(ActionExecutionContext context)
        {
#if WinRT
            var guardName  = "Can" + context.Method.Name;
            var targetType = context.Target.GetType();
            var guard      = targetType.GetRuntimeMethods().SingleOrDefault(m => m.Name == guardName);

            if (guard == null)
            {
                return(null);
            }
            if (guard.ContainsGenericParameters)
            {
                return(null);
            }
            if (!typeof(bool).Equals(guard.ReturnType))
            {
                return(null);
            }

            var guardPars  = guard.GetParameters();
            var actionPars = context.Method.GetParameters();
            if (guardPars.Length == 0)
            {
                return(guard);
            }
            if (guardPars.Length != actionPars.Length)
            {
                return(null);
            }

            var comparisons = guardPars.Zip(
                context.Method.GetParameters(),
                (x, y) => x.ParameterType.Equals(y.ParameterType)
                );

            if (comparisons.Any(x => !x))
            {
                return(null);
            }

            return(guard);
#else
            var guardName  = "Can" + context.Method.Name;
            var targetType = context.Target.GetType();
            var guard      = targetType.GetMethod(guardName);

            if (guard == null)
            {
                return(null);
            }
            if (guard.ContainsGenericParameters)
            {
                return(null);
            }
            if (typeof(bool) != guard.ReturnType)
            {
                return(null);
            }

            var guardPars  = guard.GetParameters();
            var actionPars = context.Method.GetParameters();
            if (guardPars.Length == 0)
            {
                return(guard);
            }
            if (guardPars.Length != actionPars.Length)
            {
                return(null);
            }

            var comparisons = guardPars.Zip(
                context.Method.GetParameters(),
                (x, y) => x.ParameterType == y.ParameterType
                );

            if (comparisons.Any(x => !x))
            {
                return(null);
            }

            return(guard);
#endif
        }