示例#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.GetPropertyCaseInsensitive(pair.Name);
                        if (property == null)
                        {
                            continue;
                        }

                        property.SetValue(viewModel, MessageBinder.CoerceValue(property.PropertyType, pair.Value, null));
                    }
                }
            }

            else if (dictionaryParameter != null)
            {
                foreach (var pair in dictionaryParameter)
                {
                    var property = viewModelType.GetPropertyCaseInsensitive(pair.Key);
                    if (property == null)
                    {
                        continue;
                    }

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

                property.SetValue(viewModel, MessageBinder.CoerceValue(property.PropertyType, parameter, null));
            }
        }
示例#2
0
        /// <summary>
        /// A static helper that can be used as prepare action in order to inject a parameter into the target view model
        /// </summary>
        /// <param name="target">The target view model</param>
        /// <param name="parameter">The parameter value to be injected.</param>
        /// <param name="propertyName">The name of the property to inject to.</param>
        public static void TryInjectParameter(object target, object parameter, string propertyName = "Parameter")
        {
            var viewModelType = target.GetType();
            var property      = viewModelType.GetPropertyCaseInsensitive(propertyName);

            if (property == null)
            {
                return;
            }

#if SILVERLIGHT
            property.SetValue(target, MessageBinder.CoerceValue(property.PropertyType, parameter, null), null);
#else
            property.SetValue(target, MessageBinder.CoerceValue(property.PropertyType, parameter, null));
#endif
        }
        /// <summary>
        ///   Attempts to inject query string parameters from the view into the view model.
        /// </summary>
        /// <param name="viewModel"> The view model. </param>
        /// <param name="page"> The page. </param>
        protected virtual void TryInjectQueryString(object viewModel, Page page)
        {
            var viewModelType = viewModel.GetType();

            foreach (var pair in page.NavigationContext.QueryString)
            {
                var property = viewModelType.GetPropertyCaseInsensitive(pair.Key);
                if (property == null)
                {
                    continue;
                }

                property.SetValue(
                    viewModel,
                    MessageBinder.CoerceValue(property.PropertyType, pair.Value, page.NavigationContext),
                    null
                    );
            }
        }