Exemplo n.º 1
0
        /// <summary>
        /// Attempts to inject query string parameters from the view into the view model.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="parameter">The parameter.</param>
        protected virtual void TryInjectParameters(object viewModel, object parameter)
        {
            var viewModelType = viewModel.GetType();

            var stringParameter     = parameter as string;
            var dictionaryParameter = parameter as IDictionary <string, object>;

            if (stringParameter != null && stringParameter.StartsWith("caliburn://"))
            {
                var uri = new Uri(stringParameter);

                if (!string.IsNullOrEmpty(uri.Query))
                {
                    var decorder = new WwwFormUrlDecoder(uri.Query);

                    foreach (var pair in decorder)
                    {
                        var property = viewModelType.GetRuntimeProperty(pair.Name);
                        if (property == null)
                        {
                            continue;
                        }

                        property.SetValue(viewModel,
                                          ParameterBinder.CoerceValue(property.PropertyType, pair.Value));
                    }
                }
            }
            else if (dictionaryParameter != null)
            {
                foreach (var pair in dictionaryParameter)
                {
                    var property = viewModelType.GetRuntimeProperty(pair.Key);
                    if (property == null)
                    {
                        continue;
                    }

                    property.SetValue(viewModel,
                                      ParameterBinder.CoerceValue(property.PropertyType, pair.Value));
                }
            }
            else
            {
                var property = viewModelType.GetRuntimeProperty("Parameter");
                if (property == null)
                {
                    return;
                }

                property.SetValue(viewModel,
                                  ParameterBinder.CoerceValue(property.PropertyType, parameter));
            }
        }
Exemplo n.º 2
0
        private object[] DetermineParameters(MethodBase method, object eventArgs = null)
        {
            var requiredParameters = method.GetParameters();

            if (requiredParameters.Length == 0)
            {
                return(new object[0]);
            }

            var parameterValues = Parameters.Select(x => ((Parameter)x).Value).ToArray();

            if (requiredParameters.Length != parameterValues.Length)
            {
                throw new InvalidOperationException(string.Format("Inconsistent number of parameters for method {0}.", method.Name));
            }

            var context = (CoroutineExecutionContext)null;

            for (var i = 0; i < requiredParameters.Length; i++)
            {
                var parameterType  = requiredParameters[i].ParameterType;
                var parameterValue = parameterValues[i];

                var specialValue = parameterValue as ISpecialValue;
                if (specialValue != null)
                {
                    if (context == null)
                    {
                        context = new CoroutineExecutionContext
                        {
                            Source    = AssociatedObject,
                            Target    = Target,
                            EventArgs = eventArgs,
                        }
                    }
                    ;

                    parameterValue = specialValue.Resolve(context);
                }

                parameterValues[i] = ParameterBinder.CoerceValue(parameterType, parameterValue);
            }

            return(parameterValues);
        }
Exemplo n.º 3
0
        private TParameter CoerceParameter(object parameter)
        {
            if (parameter == null)
            {
                return(default(TParameter));
            }

            var specialValue = parameter as ISpecialValue;

            if (specialValue != null)
            {
                parameter = specialValue.Resolve(new CoroutineExecutionContext());
            }

            if (parameter is TParameter)
            {
                return((TParameter)parameter);
            }

            return((TParameter)ParameterBinder.CoerceValue(typeof(TParameter), parameter));
        }