/// <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 dictionaryParameter = parameter as IDictionary <string, object>; 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), null); } } else { var property = viewModelType.GetPropertyCaseInsensitive("Parameter"); if (property == null) { return; } property.SetValue(viewModel, MessageBinder.CoerceValue(property.PropertyType, parameter, null), null); } }
/// <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 = HttpUtility.ParseQueryString(uri.Query); foreach (var pair in decorder) { var property = viewModelType.GetPropertyCaseInsensitive(pair.Key); 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)); } }
protected virtual void TryInjectParameter(object viewModel, object parameter) { var viewModelType = viewModel.GetType(); var property = viewModelType.GetPropertyCaseInsensitive("Parameter"); if (property == null) { return; } property.SetValue(viewModel, MessageBinder.CoerceValue(property.PropertyType, parameter, null)); }
/// <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 ); } }
/// <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(); if (parameter is string && ((string)parameter).StartsWith("caliburn://")) { var uri = new Uri((string)parameter); 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 { var property = viewModelType.GetPropertyCaseInsensitive("Parameter"); if (property == null) { return; } property.SetValue(viewModel, MessageBinder.CoerceValue(property.PropertyType, parameter, null)); } }